Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel: Adding relationships to a pivot model

How can I add a relationship to a pivot Model?

I have the following tables:

users
-id

events
-id

user_event
-id
-user_id
-event_id

tickets
-price
-user_event_id

So one User could visit many events and one Event belongs to many Users. Now I want that a User could have many different tickets for one special Event.

My models are the following:

Event:

class Event extends Eloquent{
    // Joins
    public function getUsers(){
        return $this->belongsToMany('User', 'user_event', 'event_id', 'user_id')->withPivot('id', 'emailSent', 'info')->withTimestamps();
    }
    // Pivot
    public function newPivot(Eloquent $parent, array $attributes, $table, $exists) {
       if ($parent instanceof User) {
            return new UserEvent($parent, $attributes, $table, $exists);
       }
       return parent::newPivot($parent, $attributes, $table, $exists);
    }

User:

class User extends Eloquent implements UserInterface, RemindableInterface {
    //Joins  
    public function getEvents(){
        return $this->belongsToMany('Event', 'user_event', 'user_id', 'event_id')->withPivot('id', 'emailSent', 'info')->withTimestamps();
    }


    // Pivot
    public function newPivot(Eloquent $parent, array $attributes, $table, $exists) {
        if ($parent instanceof Event) {
            return new UserEvent($parent, $attributes, $table, $exists);
        }
        return parent::newPivot($parent, $attributes, $table, $exists);
    }

UserEvent

use Illuminate\Database\Eloquent\Relations\Pivot;

class UserEvent extends Pivot{
    protected $table = 'user_event';
    public function tickets() {
        return $this->hasMany('Ticket'); 
    }
}

Ticket

class Ticket extends Eloquent{
    // Joins
    public function getUserEvent(){
        return $this->belongsTo('user_event','user_event_id');
    }

Now I want to list the first ticket for one specific User to one specific Event: I tried the following:

$event->first()->getUsers->first()->pivot->tickets->first();

But I get an error message:

Call to a member function first() on a non-object

I don't know where I should look to solve that problem. I've already looked to

  • https://github.com/laravel/framework/issues/2093#issuecomment-39154456

  • Using pivot model data as a relationship to another model in Laravel 4's Eloquent

but it didn't solved my problem.

Any ideas? Or isn't that possible with Eloquent?

like image 820
Tob0t Avatar asked Nov 10 '22 05:11

Tob0t


1 Answers

The error you get is probably caused by some typo (->getUsers->first() part I suppose), because the code you pasted should throw DB error unknown column event_id when it reaches ->tickets part.

So first solve this issue, and then:

to make it work, you need to specify foreign key in the relation, because Eloquent guesses it using getForeignKey() which is overriden on the Pivot class and returns different value (event_id in this case):

class UserEvent extends Pivot{
    protected $table = 'user_event';
    public function tickets() {
        return $this->hasMany('Ticket', 'user_event_id'); 
    }
}
like image 68
Jarek Tkaczyk Avatar answered Nov 14 '22 23:11

Jarek Tkaczyk