30 Days to Learn Laravel, Ep 12 - Pivot Tables and BelongsToMany Relationships

Поделиться
HTML-код
  • Опубликовано: 2 окт 2024
  • The next key Eloquent relationship type to understand is belongsToMany(). You will reach for this type whenever you're working with pivot tables. We'll use the example of jobs and tags to illustrate the basic concept.
    Watch Full Series on: laracasts.com/...
    Watch thousands of videos, track your progress, and participate in a massive Laravel community at Laracasts.com.
    Laracasts: laracasts.com
    Laracasts Twitter: / laracasts
    Jeffrey Way Twitter: / jeffrey_way

Комментарии • 26

  • @GilbertKarogoKabui
    @GilbertKarogoKabui 10 дней назад

    Wow you really make it easy to understand compared to other sources. Continue the good work.

  • @koertved
    @koertved 5 месяцев назад +6

    I really love laracast because even though I've been using laravel for 3 years, I still learn new things and tips in this series. Great job!

    • @i.t.starters2019
      @i.t.starters2019 5 месяцев назад

      Definitely, me too, I've been doing laravel for over 3 years but I've never really known how relationships, pivot tables actually work, and now I feel like a beginner 😅

  • @paulholsters7932
    @paulholsters7932 2 месяца назад +1

    Really good video's. But I prefer edgeDB when it comes to relationships (and ORM's in general). So much simpler. So much less code to write! Just learning Laravel to get a job.

  • @diegosk8zero
    @diegosk8zero 5 месяцев назад +1

    Laravel is life

  • @SatoKazumaLvl
    @SatoKazumaLvl 5 месяцев назад

    Pheww. Everytime I finish an episode I feel like I level up 10 times

  • @edorourke9223
    @edorourke9223 3 месяца назад +2

    FYI - Tried the rollback and migration in the VSC terminal, and got the following error:
    "The token '&&' is not a valid statement separator in this version."
    Tried again in the cmd window and it was fine

    • @Cellohhh
      @Cellohhh Месяц назад

      You saved me, thanks!

  • @sale7680
    @sale7680 28 дней назад

    ❤❤❤

  • @axljoven984
    @axljoven984 3 месяца назад +1

    Hi! Should pivot table names singular? e.g., job_tag instead of job_tags? I encountered a constraint violation error when I unknowingly job_tags.

    • @renatofrota
      @renatofrota 2 месяца назад +1

      the convention for pivot tables is to name it accordingly to the model names (singular_singular), in alphabetical order. so a pivot for Posts (model Post) and Categories (model Category) would be category_post.

    • @axljoven984
      @axljoven984 2 месяца назад +1

      @@renatofrota I see. That makes sense. Thanks!

  • @sale7680
    @sale7680 28 дней назад

    Best best

  • @jeremymattausch2218
    @jeremymattausch2218 3 месяца назад +1

    There is a little something I have trouble understanding.
    You write, as relatedPivotKey and foreignPivotKey (in the Job/Tag models) for it to be "job_listing_id".
    Whereas, in your database (and everywhere else), it's clearly written "job_listings_id" with an "S" at listing. How come your query works with the table name not matching ? I tried without the S myself and it trhew an error not finding "job_listing_id" (which is normal, since in my database, just like yours, it's "job_listings_id" instead.

    • @renatofrota
      @renatofrota 2 месяца назад

      the table name is "job_listings" (it is what it represents: a collection of job listings) as like the "tags" table is a collection of tags. the other hand, the columns at the pivot table job_tag holds the id of a particular listing (job_listing_id, in singular form) and the id of a particular tag (tag_id, also in singular form).

  • @EnimaIffets
    @EnimaIffets 5 месяцев назад

    nice

  • @jerryjohnson5140
    @jerryjohnson5140 4 месяца назад

    I've been debugging for some hours. I've checked and rechecked my code, but seems like my pivot table relationship is not similar as yours. Mine don't have the primary key for the job_listing_id and the tag_id

    • @jerryjohnson5140
      @jerryjohnson5140 4 месяца назад

      5:57

    • @wuba6977
      @wuba6977 4 месяца назад

      ​@@jerryjohnson5140 Is just a structure but basically when you do $table->foreignIdFor(Model Class) which is in the video is Job::class, after migration, the column will be like this job_id, but since sir Jeffrey override it, it became like this job_listing_id. if you're inside job_tags table, the job_listing_id and tag_id are FK not PK.
      Maybe I miss something. You can reply your problem here so that others can also answer.

    • @jeremymattausch2218
      @jeremymattausch2218 3 месяца назад

      I'm gonna guess you fixed that already, but if not : It probably comes from your migration "create_tags_table" where he puts 2 create in there (4:43 timestamp). This is what generates the fields in the DB if I'm not mistaken.
      If you've got it already, did you migrate:refresh ? If you did, did you CTLR + R while on TablePlus to refresh the DB tables/fields ?
      Cheers

  • @brothercaleb
    @brothercaleb 5 месяцев назад

    Awesome