Laravel hasManyThrough 等价物:belongsTo 关系通过另一个模型
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/23365905/
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 hasManyThrough equivalent: belongsTo relationship through another model
提问by BenjaminRH
I've got a model, it belongs to another model, that model belongs to a third model, and I want an eloquent method to relate the first model to the third one.
我有一个模型,它属于另一个模型,该模型属于第三个模型,我想要一种雄辩的方法将第一个模型与第三个模型联系起来。
There doesn't appear to be a belongsToThrough (or hasOneThrough) method, though. I've already tried chaining multiple belongsTo
methods, but that hasn't worked (Call to undefined method Illuminate\Database\Query\Builder::belongsTo()
). Any ideas?
不过,似乎没有belongsToThrough(或hasOneThrough)方法。我已经尝试过链接多种belongsTo
方法,但没有奏效 ( Call to undefined method Illuminate\Database\Query\Builder::belongsTo()
)。有任何想法吗?
Here is an example of the models:
以下是模型的示例:
// The first model
// Schema: this model has a middle_id column in the database
class Origin extends Eloquent {
public function middle()
{
return $this->belongsTo('Middle');
}
}
// The second model
// Schema: this model has a target_id column in the database, but NOT an origin_id column
class Middle extends Eloquent {
public function target()
{
return $this->belongsTo('Target');
}
}
// The third model
class Target extends Eloquent {
}
What I'd like to do is add something like the following method to the Origin
model:
我想做的是向Origin
模型添加类似以下方法的内容:
// A relationship method on the first "origin" model
public function target()
{
// First argument is the target model, second argument is the middle "through" model, third argument is the database column in middle model that it uses to find the target model, or soemthing
return $this->hasOneThrough('Target', 'Middle', 'target_id');
}
So that I can use $originInstance->target->title
, etc.
这样我就可以使用$originInstance->target->title
,等等。
回答by Razor
public function target() {
$middle = $this->belongsTo('Middle','middle_id');
return $middle->getResults()->belongsTo('Target');
}
回答by Mladen Janjetovic
If this is situation like message in a bottle, and bottle is owned by the user(user > bottle > message
)
如果这是瓶中消息的情况,并且瓶子归用户所有( user > bottle > message
)
The only way I know to get the relation object is:
我知道获取关系对象的唯一方法是:
// THIS IS IN App\Message
public function bottle()
{
return $this->belongsTo('App\Bottle');
}
public function user()
{
return $this->bottle->belongsTo('App\User');
}
回答by Marek Gralikowski
You can use hasOneThrough
but you need to customize keys.
您可以使用,hasOneThrough
但您需要自定义密钥。
public function parent()
{
return $this->hasOneThrough(Parent::class, Middle::class, 'id', 'id', 'middle_id', 'parent_id');
}
Origin belongs to Middle, and Middle belongs to Parent. Middle need has parent_id
foreign key, and Origin has middle_id
foreign key.
Origin属于Middle,Middle属于Parent。中间需要有parent_id
外键,Origin有middle_id
外键。
Finally you can use:
最后你可以使用:
Origin::find(1)->parent;
回答by Jon
// First Model
public function secondModelRelation()
{
return $this->belongsTo('App\Models\SecondModel');
}
public function thirdModelRelation()
{
// Call the thirdModelRelation method found in the Second Model
return $this->secondModelRelation->thirdModelRelation;
}
// Second Model
public function thirdModelRelation()
{
return $this->belongsTo('App\Models\ThirdModel');
}
// Third Model