简体   繁体   English

在laravel模型中处理关系

[英]Handling relationship in model in laravel

I am learning relationships in Laravel php framework and I am trying to build this query 我正在学习Laravel php框架中的关系,我正在尝试构建此查询

SELECT * FROM users u INNER JOIN link_to_stores lts ON u.id=lts.user_id INNER JOIN stores s ON lts.store_id=s.store_id WHERE lts.privilege = 'Owner'

I built this in Model 我在模型中构建了这个

Link_to_store.php Link_to_store.php

public function store()
{
    return $this->belongsTo('App\Store');
}

public function user()
{
    return $this->belongsTo('App\User');
}

User.php user.php的

public function store_links()
{
    return $this->hasMany('App\Link_to_store');
}

Store.php Store.php

public function user_links()
{
    return $this->hasMany('App\Link_to_store');
}

I tried this query but this only joins user and link_to_store table 我尝试了这个查询,但这只加入了user和link_to_store表

$personal_stores = Auth::user()->store_links->where('privilege','=','Owner');

Now I am confused how to join store table too. 现在我也很困惑如何加入商店表。 Can anyone help with this? 有人能帮忙吗?

Schema is like this 架构是这样的

Stores Table 商店表

store_id store_name

Users Table 用户表

id name

Link_to_stores Table Link_to_stores表

id store_id user_id privilege

I suppose store_links is actually a pivot table. 我认为store_links实际上是一个数据透视表。 In this case, you can use belongsToMany() , this will automatically take care of the pivot table. 在这种情况下,您可以使用belongsToMany() ,这将自动处理数据透视表。

To do this, in your User model you change the store function to this: 为此,在User模型中将商店功能更改为:

function stores() {
    return $this->belongsToMany('App\Store', 'store_links', 'user_id', 'store_id')->withPivot('privilege');
}

Because the primary key of stores is not id , you will have to define this in you Store model with the following line: 由于stores的主键不是id ,因此您必须使用以下行在Store模型中定义它:

protected $primaryKey = 'store_id';

Now to get the stores for a user, you simply call 现在,为了获得用户的商店,您只需致电

$stores = Auth::user->stores()->wherePivot('privilege', 'Owner')->get();

I am learning relationships in Laravel php framework and I am trying to build this query 我正在学习Laravel php框架中的关系,我正在尝试构建此查询

SELECT * FROM users u INNER JOIN link_to_stores lts ON u.id=lts.user_id INNER JOIN stores s ON lts.store_id=s.store_id WHERE lts.privilege = 'Owner'

You are trying to do a join here. 你在这里试图加入。 You can do a join like this: 你可以这样做一个连接:

$stores = User::join('link_to_stores as lts', 'users.id', '=', 'lts.user_id')->join('stores as s', 'lts.store_id', '=', 's.id')->where('lts.privilege', 'Owner')->get();

But like Jerodev pointed out, it seems like Many to Many relationship might make more sense in your case. 但就像杰罗德夫指出的那样,在你的情况下,似乎多对多的关系可能更有意义。 The difference is that relationship will actually execute 2 queries (1 for original model, 1 for relationship). 不同之处在于关系实际上会执行2个查询(原始模型为1,关系为1)。 It will then attach the related models to the original model (which is extremely handy). 然后它会将相关模型附加到原始模型(非常方便)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM