Laravel - Pivot table for three models - how to insert related models?

混江龙づ霸主 提交于 2019-11-29 23:29:00

问题


I have three models with Many to Many relationships: User, Activity, Product. The tables look like id, name. And in the each model there are functions, for example, in User model:

public function activities()
{
    return $this->belongsToMany('Activity');
}
public function products()
{
    return $this->belongsToMany('Product');
}

The pivot table User_activity_product is:

id, user_id, activity_id, product_id. The goal is to get data like: User->activity->products. Is it possible to organize such relations in this way? And how to update this pivot table?


回答1:


First I suggest you rename the pivot table to activity_product_user so it complies with Eloquent naming convention what makes the life easier (and my example will use that name).

You need to define the relations like this:

// User model
public function activities()
{
    return $this->belongsToMany('Activity', 'activity_product_user');
}
public function products()
{
    return $this->belongsToMany('Product', 'activity_product_user');
}

Then you can fetch related models:

$user->activities; // collection of Activity models
$user->activities->find($id); // Activity model fetched from the collection
$user->activities()->find($id); // Activity model fetched from the db

$user->activities->find($id)->products; // collection of Product models related to given Activity
// but not necessarily related in any way to the User

$user->activities->find($id)->products()->wherePivot('user_id', $user->id)->get();
// collection of Product models related to both Activity and User

You can simplify working with such relation by setting up custom Pivot model, helper relation for the last line etc.

For attaching the easiest way should be passing the 3rd key as a parameter like this:

$user->activities()->attach($activityIdOrModel, ['product_id' => $productId]);

So it requires some additional code to make it perfect, but it's feasible.




回答2:


The solution was found with some changes. In the models relationships look like:

// User model
public function activities()
{
    return $this->belongsToMany('Activity', 'user_activity_product', 'user_id', 'activity_id')->withPivot('product_id');
}
public function products()
{
    return $this->belongsToMany('Product', 'user_activity_product', 'user_id', 'product_id')->withPivot('activity_id');
}

To update pivot table:

$user->products()->save($product, array('activity_id' => $activity->id));

- where product and activity ids I get from Input. And, for example, to check if "user -> some activity -> some product is already exists":

if ($company->activities->find($activity_id)->products()->where('product_id', '=', $product_id)->wherePivot('company_id', $company_id)->get()->count() > 0) {
            // code...
}

I think it needs improvements but it works for me now.



来源:https://stackoverflow.com/questions/23651405/laravel-pivot-table-for-three-models-how-to-insert-related-models

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!