Sql Laravel中的关系如何仅在pivot处于活动状态时列出数据
使用Laravel。我在MySQL数据库中有下面的表 infos表接受1个中介(表中介)和1个存储(表存储)。 如何从infos表中获取所有数据?但仅当数据透视表(中间存储)处于活动状态时=1Sql Laravel中的关系如何仅在pivot处于活动状态时列出数据,sql,laravel,eloquent,many-to-many,relational-database,Sql,Laravel,Eloquent,Many To Many,Relational Database,使用Laravel。我在MySQL数据库中有下面的表 infos表接受1个中介(表中介)和1个存储(表存储)。 如何从infos表中获取所有数据?但仅当数据透视表(中间存储)处于活动状态时=1 infos - id - name - intermediary_id - store_id intermediaries - id - name intermediary_store - intermediary_id - store_id - active stores - id - name
infos
- id
- name
- intermediary_id
- store_id
intermediaries
- id
- name
intermediary_store
- intermediary_id
- store_id
- active
stores
- id
- name
型号:
class Info extends Model
{
public function intermediary()
{
return $this->belongsTo('App\Intermediary');
}
public function store()
{
return $this->belongsTo('App\Store');
}
}
class Intermediary extends Model
{
public function stores()
{
return $this->belongsToMany('App\Store')->withPivot('active');
}
}
class Store extends Model
{
public function intermediaries()
{
return $this->belongsToMany('App\Intermediary')->withPivot('active');
}
}
您应该能够在查询时使用wherePivot('active',1)。你能展示你尝试过的代码和你遇到的错误吗?另外,发布模型和
belongsToMany()
关系可能会有所帮助。他们是对的吗?是的!看起来不错:)所以您应该能够使用(['intermediates'=>function($query){$query->wherePivot('active',1);}])->get()执行Store::操作
使用活动的中间层
模型获取所有存储
模型。并使用(['stores'=>function($query){$query->wherePivot('active',1);}])->get()将其反转使用活动的存储获取所有中间人
记录。但是为了得到所有的信息数据?Where(intermediate_id和store_id)应该处于活动状态=1老实说,我认为您根本不需要infos
表。将name
添加到intermediate\u store
表中,并使用pivot(['name','active'])
添加到,您就拥有了所需的一切。通常,您没有数据透视表的模型。在查询时,您应该能够使用->wherePivot('active',1)
。你能展示你尝试过的代码和你遇到的错误吗?另外,发布模型和belongsToMany()
关系可能会有所帮助。他们是对的吗?是的!看起来不错:)所以您应该能够使用(['intermediates'=>function($query){$query->wherePivot('active',1);}])->get()执行Store::操作
使用活动的中间层
模型获取所有存储
模型。并使用(['stores'=>function($query){$query->wherePivot('active',1);}])->get()将其反转
使用活动的存储获取所有中间人
记录。但是为了得到所有的信息数据?Where(intermediate_id和store_id)应该处于活动状态=1老实说,我认为您根本不需要infos
表。将name
添加到intermediate\u store
表中,并使用pivot(['name','active'])
添加到,您就拥有了所需的一切。通常,您没有数据透视表的模型。