Robo Robok
Robo Robok

Reputation: 22765

Belongs to pivot table in Laravel 5

Let's say I'm having the following database schema:

enter image description here

dogs and owners are connected with classic belongsToMany. How about the walks table? I'd like to be able to use Eloquent goodies in such kind of relationship:

$dogs = Dog::with('walks')->get();
$walks = Walk::with('dogs')->get();

In theory, I could replace dog_owner_id with two separate columns: dog_id and owner_id. That would make it easy to use Eloquent, but I'd lose some data integrity, because unrelated Dog and Owner could potentially go for a walk (and that's not safe for neither!).

In Walk.php, how should the relations be defined?

public function dog() {
    // return ???;
}

How about Dog.php and Owner.php?

public function walks() {
    // return ???;
}

Upvotes: 6

Views: 9600

Answers (2)

Esraa Gamal
Esraa Gamal

Reputation: 81

1- create 4 Models (Walk,Dog,Owner,DogOwner)

2- in Walk.php model

    public function dog()
{

    return $this->hasOneThrough(
        'App\Models\Dog',
        'App\Models\DogOwner',
        'id', // Local key on dog_owner table...
        'id', // Local key on dogs table...
        'dog_owner_id', // Foreign key on walks table...
        'dog_id' // Foreign key on dog_owner table...
    );
}

public function owner()
{

    return $this->hasOneThrough(
        'App\Models\Owner',
        'App\Models\DogOwner',
        'id', // Local key on dog_owner table...
        'id', // Local key on owners table...
        'dog_owner_id', // Foreign key on walks table...
        'owner_id' // Foreign key on dog_owner table...
    );
}       

3- in Dog.php model

   public function walks()
    {
    return $this->hasManyThrough(
        'App\Models\Walk',
        'App\Models\DogOwner',
        'dog_id', // Foreign key on dog_owner table...
        'dog_owner_id', // Foreign key on walks table...
        'id', // Local key on dogs table...
        'id' // Local key on dog_owner table...
    );
    }   

4- in Owner.php model

   public function walks()
   {
    return $this->hasManyThrough(
        'App\Models\Walk',
        'App\Models\DogOwner',
        'owner_id', // Foreign key on dog_owner table...
        'dog_owner_id', // Foreign key on walks table...
        'id', // Local key on owners table...
        'id' // Local key on dog_owner table...
    );
   }

Upvotes: 8

Stephan-v
Stephan-v

Reputation: 20329

As other have mentioned you can use HasManyThrough for this:

https://laravel.com/docs/5.5/eloquent-relationships#has-many-through

Like you said this is valid for your Dog and Owner models. For your Walk model it is a little bit different but you have the nice option of mapping the query yourself:

return $this->hasManyThrough(
    'App\Post',
    'App\User',
    'country_id', // Foreign key on users table...
    'user_id', // Foreign key on posts table...
    'id', // Local key on countries table...
    'id' // Local key on users table...
);

Upvotes: 0

Related Questions