Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/75.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sql Laravel中的关系如何仅在pivot处于活动状态时列出数据_Sql_Laravel_Eloquent_Many To Many_Relational Database - Fatal编程技术网

Sql Laravel中的关系如何仅在pivot处于活动状态时列出数据

Sql 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

使用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
型号:

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'])
添加到
,您就拥有了所需的一切。通常,您没有数据透视表的模型。