Laravel hasManyThrough equivalent: belongsTo relationship through another model

laravel wherehas
laravel polymorphic relationship example
laravel has many through pivot
laravel wherehas morph
laravel relationship tutorial
laravel morphone
eloquent hasonethrough
laravel update related 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. I've already tried chaining multiple belongsTo methods, but that hasn't worked (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:

// 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.


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

Eloquent: Relationships - Laravel, The save Method; The create Method; Belongs To Relationships; Many To Many Has Many Through; One To One (Polymorphic); One To Many (Polymorphic) child model to a different column, you may pass a third argument to the belongsTo A one-to-one polymorphic relation is similar to a simple one-to-one relation;  With Laravel 4.1, we can now use hasManyThrough to skip Category laravel-hasmanythrough-equivalent-belongsto-relationship-through-another-model with another


If this is situation like message in a bottle, and bottle is owned by the user (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');
}

[Proposal] Belongs to through · Issue #3052 · laravel/framework , GitHub is home to over 50 million developers working together to host I'd like to see a belongsToThrough relationship for models that work like -​hasmanythrough-equivalent-belongsto-relationship-through-another-model. Laravel only provide 2 relationship types out of the box, that supports more than one level of related models: hasOneThrough and hasManyThrough. They are not truly “multilevel” either, as they only support two levels. hasOneThrough allows you to define relationships like “A Country has one NationalFlag through Constitution.”.


You can use hasOneThrough but you need to customize keys.

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.

Finally you can use:

Origin::find(1)->parent;

Inverse of Eloquent hasManyThrough relationship · Issue #1170 , GitHub is home to over 50 million developers working together to host and review Inverse of Eloquent hasManyThrough relationship #1170 would generate a query similar to IE: having a message model and getting the user. The inverse of a hasMany to hasMany is a belongsTo to belongsTo . Has Many Through relationship is a bit complicated to understand a provide shortcut way to access data of another mode relation. For example, a country is connected with users and users with posts, then we can access all posts connected with a specific country.


// 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

hasManyThrough with intermediate belongsTo, I have a database structure which boils down to a CertificateType which has User are related by a belongsTo relationship, I cannot use hasManyThrough as such: look at each user using $this->certificates()->with('user') or something similar to just create a ManyToMany relationship on the CertificateType model, using  hasManyThrough Relationship for Eloquent laravel 4 Tag: laravel-4 , eloquent After a lot of searching i couldn't find a real solution to my "problem", so if there any one how can help me, i’am all listening


A Guide On Laravel Relationships - The Startup, We let the Passport model know that it belongs to a User. We do Linking a model through another model The “has many through” relationship is the equivalent of the “has one through” relationship, but for multiple records. Has One Through. The "has-one-through" relationship links models through a single intermediate relation. For example, in a vehicle repair shop application, each Mechanic may have one Car, and each Car may have one Owner. While the Mechanic and the Owner have no direct connection, the Mechanic can access to the Owner through the Car itself. Let's look at the tables necessary to define this relationship:


Laravel (5.7) Eloquent Relationships, We define eloquent relationships as methods on our Eloquent model classes. model to a different column, we may pass a third argument to the belongsTo Just like all other Eloquent relationships, one-to-many relationships are The first argument that is passed to the hasManyThrough method is the  Extending Cross-Database Relationships. If you're using a hybrid MongoDB and SQL setup, you can define relationships across them. The model will automatically return a MongoDB-related or SQL-related relation based on the type of the related model.


eloquent relationship – Jeff's Reference, Accessors and mutators allow you to format Eloquent attribute values when you retrieve or Rule is very simple and very similar to that of Accessor. Belongs To Relationships: Using process describes above, you can define Message relation to other models Laravel 5: Eloquent – relationship types: has many through. Just like with hasManyThrough (), the first argument of hasManyDeep () is the related model. The second argument is an array of intermediate models, from the far parent (the model where the relationship is defined) to the related model. By default, hasManyDeep () uses the Eloquent conventions for foreign and local keys.