Laravel5:'HasManyThrough' 使用一个模型和一个中间模型 table 而不是两个模型
Laravel5: 'HasManyThrough' using one model and one intermediate table instead of two models
是否可以传递中间 table 作为 hasManyThrough()
的第二个参数?我只想使用 table,而不是模型。它应该是这样的:
return $this->hasManyThrough(
'App\Post',
'database.table', <- table instead of model
'country_id',
'user_id',
'id',
'id'
);
Eloquent 关系不可能做到这一点。
实现您想要的最简单方法是创建一个新模型,该模型除了引用您的 table 名称外什么都不做:
class Example extends Model
{
protected $table = "database.table";
//or whatever the name of your table is
}
然后你可以这样做:
return $this->hasManyThrough(
'App\Post',
'App\Example',
'country_id',
'example_id',
'id',
'id'
);
是否可以传递中间 table 作为 hasManyThrough()
的第二个参数?我只想使用 table,而不是模型。它应该是这样的:
return $this->hasManyThrough(
'App\Post',
'database.table', <- table instead of model
'country_id',
'user_id',
'id',
'id'
);
Eloquent 关系不可能做到这一点。
实现您想要的最简单方法是创建一个新模型,该模型除了引用您的 table 名称外什么都不做:
class Example extends Model
{
protected $table = "database.table";
//or whatever the name of your table is
}
然后你可以这样做:
return $this->hasManyThrough(
'App\Post',
'App\Example',
'country_id',
'example_id',
'id',
'id'
);