Laravel Eloquent:关系的多个外键
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/48077890/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Laravel Eloquent: multiple foreign keys for relationship
提问by Genti Saliu
I am in the process of porting a project to Laravel.
我正在将项目移植到 Laravel。
I have two database tables which are in a One-To-Many relationship with each other. They are joined by three conditions. How do I model this relationship in Eloquent?
我有两个数据库表,它们之间是一对多的关系。它们由三个条件连接。我如何在 Eloquent 中为这种关系建模?
I am not supposed to modify the database schema, since it has to remain backwards compatible with other things.
我不应该修改数据库架构,因为它必须与其他东西保持向后兼容。
I have tried the following, but it doesn't work.
我已经尝试了以下方法,但它不起作用。
The owning side:
拥有方:
use Illuminate\Database\Eloquent\Model;
class Route extends Model
{
public function trips()
{
return $this->hasMany('Trip', 'route_name,source_file', 'route_name,source_file')
}
}
The inverse side:
反面:
use Illuminate\Database\Eloquent\Model;
class Trip extends Model
{
public function routes()
{
return $this->belongsTo('Route', 'route_name,source_file', 'route_name,source_file');
}
}
Example Route
database values:
示例Route
数据库值:
id | route_name | source_file
---------------------------------------
1 | Berlin - Paris | file1.xls
2 | Madrid - London| file2.xls
3 | Berlin - Paris | file3.xls
Example Trip
database values:
示例Trip
数据库值:
id | route_name | source_file | duration
---------------------------------------------------------
1 | Berlin - Paris | file1.xls | 78
2 | Madrid - London | file2.xls | 241
3 | Berlin - Paris | file3.xls | 65
1 | Berlin - Paris | file1.xls | 95
1 | Berlin - Paris | file1.xls | 65
Route
and Trip
have other attributes, which I did not include here for brevity.
Route
并Trip
具有其他属性,为简洁起见,我没有在此处包含这些属性。
Is this possible in Eloquent?
这在 Eloquent 中可行吗?
回答by topclaudy
I had to deal with a similar problem. The solution provided by @fab won't work with eager loading because $this->source_file would be nullat the time the relationship is processed. I came up with this solution
我不得不处理类似的问题。@fab 提供的解决方案不适用于急切加载,因为在处理关系时$this->source_file将为空。我想出了这个解决方案
After installing Composhipsand configuring it in your models, you can define your relationships matching multiple columns.
安装Compoships并在您的模型中对其进行配置后,您可以定义与多个列匹配的关系。
The owning side:
拥有方:
use Illuminate\Database\Eloquent\Model;
use Awobaz\Compoships\Compoships;
class Route extends Model
{
use Compoships;
public function trips()
{
return $this->hasMany('Trip', ['id', 'route_name', 'source_file'], ['route_id', 'route_name', 'source_file']);
}
}
The inverse side:
反面:
use Illuminate\Database\Eloquent\Model;
use Awobaz\Compoships\Compoships;
class Trip extends Model
{
use Compoships;
public function route()
{
return $this->belongsTo('Route', ['route_id', 'route_name', 'source_file'], ['id', 'route_name', 'source_file']);
}
}
Compoships supports eager loading.
Compoships 支持预先加载。
回答by fab
As Jonathonalready mentioned, you could try to add an where
-clause to your relationship:
正如乔纳森已经提到的,你可以尝试where
在你的关系中添加一个-clause:
use Illuminate\Database\Eloquent\Model;
class Route extends Model
{
public function trips()
{
return $this->hasMany('Trip', 'route_name')->where('source_file', $this->source_file);
}
}
The inverse side:
反面:
use Illuminate\Database\Eloquent\Model;
class Trip extends Model
{
public function routes()
{
return $this->belongsTo('Route', 'route_name')->where('source_file', $this->source_file);
}
}
回答by rchatburn
How i would tackle this would be to do something like this
我将如何解决这个问题是做这样的事情
class A
{
public function b1()
{
return $this->hasMany('B', 'prop1', 'prop1')
}
public function b2()
{
return $this->hasMany('B', 'prop2', 'prop2')
}
public function b3()
{
return $this->hasMany('B', 'prop3', 'prop3')
}
public function getBsAttribute()
{
$data = collect([$this->b1, $this->b2, $this->b3]);
return $data->unique();
}
}
Collecting all the single relations and returning them as unique collection, and obviously doing the same for the inverse, this should give you some data to work with.
收集所有的单一关系并将它们作为唯一的集合返回,显然对逆进行同样的操作,这应该会给你一些数据来处理。
OP was modified so no longer relevant, left in if any one needs an answer similar
OP 已修改,因此不再相关,如果有人需要类似的答案,请保留