欢迎各位兄弟 发布技术文章

这里的技术是共享的

You are here

Pivot tables and many-to-many relationships 多对多 关联 关系

Pivot tables and many-to-many relationships

Today I want to talk about a feature of Laravel which is really useful but can be potentially difficult to understand at first. Pivot table is an example of intermediate table with relationships between two other “main” tables.

Real-life example of pivot tables

In official documentation they show the example of User-Role relationships, where user potentially can belong to several roles, and vice versa. So to make things clearer – let’s take another real-life example: Shops and Products.

Let’s say a company has a dozen of Shops all over city/country and a variety of products, and they want to store the information about which Product is sold in which Shop. It’s a perfect example of many-to-many relationship: one product can belong to several shops, and one shop can have multiple products.

So here’s a potential database structure:
shops
– id
– name

products
– id
– name

product_shop
– product_id
– shop_id

The final table in the list – product_shop is called a “pivot” table, as mentioned in the topic title. Now, there are several things to mention here.

  • Name of the pivot table should consist of singular names of both tables, separated by undescore symbole and these names should be arranged in alphabetical order, so we have to have product_shop, not shop_product.

  • To create a pivot table we can create a simple migration with artisan make:migration or use Jeffrey Way’s package Laravel 5 Generators Extended where we have a command artisan make:migration:pivot.

  • Pivot table fields: by default, there should be only two fields – foreign key to each of the tables, in our case product_id and shop_id. You can add more fields if you want, then you need to add them to relationship assignment – we will discuss that later.

Models for Many-to-Many Relationships: BelongsToMany

Ok, we have DB tables and migrations, now let’s create models for them. The main part here is to assign a many-to-many relationship – it can be done from either of “main” tables models.

So, option 1:
app/Shop.php:



Or option 2:
app/Product.php:



Actually, you can do both – it depends on how will you actuall use the relationship in other parts of the code: will you need $shop->products or more likely to query $product->shops, or both.

Now, with such declaration of relationships Laravel “assumes” that pivot table name obeys the rules and is product_shop. But, if it’s actually different (for example, it’s plural), you can provide it as a second parameter:



Moreover, you can specify the actual field names of that pivot table, if they are different than default product_id and shop_id. Then just add two more parameters – first, the current model field, and then the field of the model being joined:



One of the main benefits here: you don’t need to create a separate model for ProductShop – you will be able to manage that table through pivot commands, we will discuss that right away.

Have you tried our tool to generate Laravel adminpanel without a line of code?
Go to QuickAdminPanel.com

Managing Many-to-Many Relationships: attach-detach-sync

So, we have tables, and we have Models ready. Now, how do we actually save the data with a help of our two Models instead of the third intermediate one? Couple of things here.

For example, if we want to add another product to the current shop instance, we use relationship function and then method attach():



The result – a new row will be added to product_shop table, with $product_id and $shop_idvalues.

Likewise, we can detach a relationship – let’s say, we want to remove a product from the shop:



Or, more brutally, remove all products from a particular shop – then just call method without parameters:



You can also attach and detach rows, passing array of values as parameters:



And another REALLY useful function, in my experience, is updating the whole pivot table. Really often example – in your admin area there are checkboxes for shops for a particular product, and on Update operation you actually have to check all shops, delete those which are not in new checkbox array, and then add/update existing ones. Pain in the neck.

Not anymore – there’s a method called sync() which accept new values as parameters array, and then takes care of all that “dirty work” of syncing:



Result – no matter what values were in product_shop table before, after this call there will be only three rows with shop_id equals 1, 2, or 3.

Additional Columns in Pivot Tables

As I mentioned above, it’s pretty likely that you would want more fields in that pivot tables. In our example it would make sense to save the amount of productsprice in that particular shop and timestamps. We can add the fields through migration files, as usual, but for proper usage in relationships we have to make some additional changes to Models:



As you can see, we can add timestamps with a simple method withTimestamps and additional fields are added just as parameters in method withPivot.

Now, what it gives us is possibility to get those values in our loops in the code. With a property called pivot:



Basically, ->pivot represents that intermediate pivot table, and with this we can access any of our described fields, like created_at, for example.

Now, how to add those values when calling attach()? The method accept another parameter as array, so you can specify all additional fields there:



Conclusion

So, pivot tables and many-to-many relationships are handled quite conveniently with Eloquent, so there’s no need to create a separate model for intermediate table. Hope that helps!

Liked the article? Maybe you need help with your Laravel project? 
Check out my Upwork profile and hire me! 

57 thoughts on “Pivot tables and many-to-many relationships

  1. Hi, want to thank you for such a great article on the topic.

    Also, it would be nice to explain how to do aggregates of the pivot data, like to sum the quantity of products sold on many shops or the count for those products.

    Thanks


    1. True that — not sure why the laravel community can’t call something by the same name that the rest of the IT world has been using for decades.

  1. Very nice article! thanks.
    It’d be nice to explain how to get and update specific fields in an specific product_shop row.
    For example, I want to update the price in a shop whom ID is 4.

  2. Ok, this is awesome. I’ve learned a lot here… but I have a question…

    So, say I have multiple stores, each with the same product, but each store has a different price.

    How would I query store 1 for product 1 only – and use the pricing from the pivot table?

    I can get a list of all the stores. I can list all the products for a single store… but I need to show a single product from a specific store.

    1. Not really, it just shows how to add extra data to the pivot table, not how to fetch specific row from it.

      @John: The relationship returns a Collection, so you should be able to use the method ->get(). i.e.: to get the price from a desired product in a shop you could do:

      $shop->products->get($product_id)->pivot->price;

  3. I am a beginner in the laravel 5 framework. I am trying to work with eloquent model relationships and not able to get the desired output.

    I have created 3 tables:
    user(id,name)
    company(c_id, company_name)
    usercompany (id,c_id) this is the PIVOT TABLE (many to many)

    I want to retrieve all the data from user and company table using the pivot table: usercompany

    THE MyUser Model:

    class MyUser extends Model
    {
    protected $table = ‘user’;
    public function getCompany()
    {
    return $this->belongsToMany(‘App\MyCompany’, ‘usercompany’, ‘id’, ‘c_id’);
    }
    }

    THE MyCompany Model:

    class MyCompany extends Model
    {

    protected $table = ‘company’;
    public function getUser()
    {
    return $this->belongsToMany(‘App\MyUser’, ‘usercompany’, ‘id’, ‘c_id’);
    }
    }

    The Controller:

    class MyUserCompanyController extends Controller
    {

    public function companys()
    {
    $companys = MyUser::all();
    return View::make(‘companys’)->with(‘companys’,$companys);
    }
    }

    The View:

    @foreach ($companys as $company)

    {{ $company->getCompany->company_name}}

    @endforeach

    The Route:

    Route::get(‘/companys’,’MyUserCompanyController@companys’);

    I am getting the following Error:

    ErrorException in Connection.php line 651: SQLSTATE[42S22]: Column not found: 1054 Unknown column ‘company.id’ in ‘on clause’ (SQL: select company.*, usercompany.id as pivot_idusercompany.c_id as pivot_c_id from company inner join usercompany on company.id = usercompany.c_id where usercompany.id = 1) (View: C:\xampp\htdocs\fundhound_final\resources\views\companys.blade.php)

    Thanks in advanced.

  1. hi
    thanks for your lovely article.
    i’m beginner in the laravel and i want to implements a many to many relationship with 3 or more entity in laravel but i don’t know how to do it.
    i’m very glad to help me.
    thanks again.

  1. Thank you so much. The eloquent documentation is not clear about this and I believe there’re errors in there too. I’m new to laravel and in the docs, that part seem disjointed, but here it makes sense. It’s not even clear on “alphabetical” blah blah, and not clear on the goal of the example. There’re no table examples, like you did and the author assumed that the reader already understands the concepts; pivot tables. I was reading it just now and thought ahhhhhh this is now confusing me let me find out more from someone else and here I’m… Brilliant!
    Whoever wrote that documentation part needs to revise it or as I noticed, documentation has git repo, which I think needs updating to clear this. Please correct it for others and ourselves so that we have a clearer documentation of this to refer to.

  1. Thank you so much for posting a great article. I am getting some errors while updating the pivot table content. Will you please solve this. Thanking you.

  2. I have a hasMany relationship as below.
    A Projectcampaign has many breakupcost and I have a page from where I want to add breakupcost. I have +Add more button in that, A user can add his desired rows of breakupcost.

    I have same form for add and edit, Is there any sync method for doing the same, if the breakupcost exist and it will not insert that again and autoremove a breakupcost If user deleted it by -Delete button.

    Please help in the same

  3. How we can send the result to view ????
    i have 2 tables channels and roups (channel can be in many groups and group can have many channels)
    public function detailGroup($id) {
    $chanelsName = array();

    $chanelsName[] = Group::find($id)->channels->each(function($channel) {
    return $channel->name;
    });
    var_dump($chanelsName[0][0]->name);
    //$group = compact(‘group’);
    //echo $chanelsName[0];
    //return view(‘admin.groups.group’, compact(‘group’), $chanelsName );

  1. If you arrive here – as I did a week ago – not fully understanding when you would need to use sync() – BOOKMARK THIS PAGE and read it every day! Managing dynamic updates to many-to-many relationships can be extremely hard to get your head around – at least it was for me. This article gave me the ‘eureka’ moment.

  2. How you can add one product in multiple shops at a time? Should we loop through each shop and add the product or is there any other way of using that in single query like we are attaching multiple products in single shop.

  3. Thank you very much for the article Povilas!
    I am new to Laravel and spent about a week trying to filter a collection by additional fields in the pivot table, before I came across your blog.

  4. Very nice article…thanks for sharing Povilas.

    I’m a newbie in laravel and currently working on authentication part of one of my projects.
    I’m using laravel 5.

    I’ve multiple users like employees, clients, admin and super-admin. Employees and Client have different info, but admin and super-admin share same info. So I ‘m trying to use polymorphic Many to Many relationship by using different tables/models for Users(admin & super-admin combined into one table), Employees, Clients, Role and Roleable table.

    I just wanted to know whether above structure is correct or not ,if in future, more users-types/roles are added to the system.

    Also wanted to know one more thing: What is the difference between Many-To-Many and Polymorphic Many-To-Many relationships. I read many blogs, went through lots of articles and codes, but couldn’t understand this difference.

    It will be really great if you can clear my doubts.

    Thanks,
    Dipti

  5. In Model, how do you calculate those amount and price from pivot table? So you have it with one query.

  1. Very good article as always !!
    I am new to Laravel. I do a lot search on relationship but still a point is not clear to me. Mostly all the examples of relationships are on single object like $shop = Shop::find($shop_id);; What is we have a object that returns multiple rows like $shops = Shop::where(‘status’, ‘=’ ,1); should we still use relationship inside the $shops loop like following:
    foreach($shops as $shop){
    $products = $shop->products();
    }

    OR we should use Joins on query builder in this situation. What is the best way?
    As per my understanding if we call relationship inside the loop then it’s the same like we are executing a database query inside a loop which is wrong way.

    foreach($shops as $shop){
    $products=Product::where(‘shop_id’,’=’,$shop->id)
    }

    Please guide me and let me know if we should joins in this situation and relationships are only good if we have a single row object.

  2. public function products()
    {
    return $this->belongsToMany(‘App\Products’, ‘products_shops’);
    }

    seems like you meant shops() in the method name or ‘App\Shops’ in the relationship class.

  3. Hi everyone,

    What about timestamps() in the pivot table ? By default, i believe that they’re not set when you do save().
    How can we have the created_at and updated_at all set when saving in the pivot table ?

  4. Hello Povilas …

    I’m beginner in Laravel and this article is very useful for me… very thanks to spend time and prepare this for us..

    It’s Excellent…

  5. Hi there can you please take a look at this problem I’m facing? For example I want to be able to select all the checkboxes of products that belongs to a shop when I try to edit a shop record, and then do the syncing method you talk about. I’v posted a thread on laracasts.com and stackoverflow but haven’t gotten any real response. Can you please take a look? https://laracasts.com/discuss/channels/general-discussion/display-values-that-are-checked-when-editing-in-laravel https://stackoverflow.com/questions/44941971/display-values-that-are-checked-when-editing-in-laravel

  6. Hi, this is the best article and a very simple to understands. Thanks a lot for take your time and explain it to IT community.

    I have a question: How can I add a new SHOP and related it to products?

    I have:

    $shop = Shop::find(1);

    $product = new Product([‘name’ => ‘xxx’]);
    $shop->products()->save($product);

  1. How do you list all shops using query builder that has only this products? [‘tv’, ‘radio’, ‘dvd_player’, ‘chromcast’]

  2. Hi, I have a peculiar situation where I have ‘courses’ and ‘students’ tables with a pivot of ‘course_student’. In the pivot table I was able to add additional columns ‘enroll_date’ and ‘grade’ successfully.
    Now I want to COUNT(*) students and groupBy(YEAR(enroll_date)) for a single course. I don’t know how else to go about it.
    I’d appreciate any help.

  3. Wow, thank you so much. I was using justly the model to pivot table and I wasn’t achieving success when I was realizing the query. Now works fine. Thanks =)

Leave a Reply

来自  http://laraveldaily.com/pivot-tables-and-many-to-many-relationships/

普通分类: