简体   繁体   English

Laravel hasManyThrough 等价物:belongsTo 关系通过另一个模型

[英]Laravel hasManyThrough equivalent: belongsTo relationship through another model

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.不过,似乎没有belongsToThrough(或hasOneThrough)方法。 I've already tried chaining multiple belongsTo methods, but that hasn't worked ( Call to undefined method Illuminate\\Database\\Query\\Builder::belongsTo() ).我已经尝试链接多个belongsTo方法,但没有奏效( Call to undefined method Illuminate\\Database\\Query\\Builder::belongsTo() )。 Any ideas?有任何想法吗?

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等。

public function target() { 
    $middle = $this->belongsTo('Middle', 'middle_id'); 
    return $middle->getResults()->belongsTo('Target'); 
}

Update:更新:

Starting from laravel 5.8 you can use the hasOneThrough relationship:从 laravel 5.8 开始,你可以使用hasOneThrough关系:

public function target() { 
    return $this->hasOneThrough('Target', 'Middle');
}

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');
}

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. Origin属于Middle,Middle属于Parent。 Middle need has parent_id foreign key, and Origin has middle_id foreign key. Middle 需要有parent_id外键,Origin 有middle_id外键。

Finally you can use:最后你可以使用:

Origin::find(1)->parent;
// 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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM