Php Laravel与多个列有许多关联
我有两种型号Php Laravel与多个列有许多关联,php,laravel,laravel-4,laravel-5,eloquent,Php,Laravel,Laravel 4,Laravel 5,Eloquent,我有两种型号 model_1 model_2 model_1有许多model_2 现在,我想将模型1与多个模型2进行多列匹配 让我举一个原始查询的例子 select ...... from model_1 left join model_2 ON (model_1.f1 = model_2.f1 AND model_1.f2 = model_2.f2) 在有许多协会的情况下如何做到这一点官方的Laravel文件总结说: 我通过在模型中添加一个附加参数来实现该功能: public functi
model_1
model_2
model_1
有许多model_2
现在,我想将模型1与多个模型2进行多列匹配
让我举一个原始查询的例子
select ...... from model_1 left join model_2 ON (model_1.f1 = model_2.f1 AND model_1.f2 = model_2.f2)
在
有许多协会的情况下如何做到这一点官方的Laravel文件总结说:
我通过在模型中添加一个附加参数来实现该功能:
public function my_joined_columns($mySecondJoinColumnValue)
{
return $this->hasMany('NamespaceToOtherModel', 'myIdColumn')
->where('my_second_join_column', $mySecondJoinColumnValue);
}
然后我确保传入参数:
MyModel1::find(1)->my_joined_columns(2)->get()
我在处理预先存在的模式时遇到了这种情况。我想出了
在model_1和model_2中安装并配置它之后,您可以定义匹配多个列的关系
在型号1中:
public function model_2()
{
return $this->hasMany(model_2::class, ['f1', 'f2'], ['f1', 'f2']);
}
在型号2中:
public function model_1()
{
return $this->belongsTo(model_1::class, ['f1', 'f2'], ['f1', 'f2']);
}
Compoships支持即时加载