0
votes

I have a Users table and an Events table.

It's has one-to-many relationship.

Each user can create many event.

Each event belongs to one user.

Also, it has many-to-many relationship.

Each user can join as many event as they want.

Each event can be joined by many user.

This needs pivot table.

Now, I'm stuck.

This is event model.

public function user(){
     return $this->belongsTo('App\User');
}

public function users(){
     return $this->belongsToMany('App\User')
                 ->withTimestamps();
}    

This is user model.

public function events(){
     return $this->hasMany('App\Event');
}

public function events(){
     return $this->belongsToMany('App\Event');
}

The problem is in the user model where I can't define multiple function with the same name.

So, is there a way to do this correctly?

2
show your events and users table as wellShailendra Gupta
I don't get this sentence of yours "The problem is in the user model where I can't define multiple function with the same name. " explain pleaseMahdi Younesi
Your relationships are correct. Just use a different name for one of the events relationships.Jonas Staudenmeir

2 Answers

0
votes

It seems like it's many to many relationships between User and Event so there will be pivot name like user_event

user model

public function events() {
    return $this->belongsToMany('App\Event')->using('App\UserEvent'); 
}

Reference: https://laravel.com/docs/5.7/eloquent-relationships#many-to-many

0
votes

Quick answer

Of course you can't have two functions with the same name. In your case, try to use more specific names for each function:

public function createdEvents()
{
     return $this->hasMany('App\Event');
}

public function joinedEvents()
{
     return $this->belongsToMany('App\Event');
}

Recommendation

You can use a single many-to-many relationship to manage both relations with Pivot information:

users table

  • id
  • username
  • ...

events table

  • id
  • name
  • ...

event_user table

  • user_id

  • event_id

  • is_creator (default FALSE, unsigned integer)

  • ...

Then when creating an event, relate the user and event objects and set the is_creator field to TRUE.

So in your User model:

app/User.php

public function events()
{
     return $this->belongsToMany('App\Event')->withPivot('is_creator');
}

Then in your controller when you want to create an event:

app/Http/Controllers/SomeCoolController.php

public function store(CreateEventRequest $request)
{
    // Get your event data
    $data = $request->only(['your', 'event', 'fields']);
    // create your object
    $newEvent = Event::create($data);
    // create the relationship with the additional pivot flag.
    auth()->user()->events()->attach($newEvent, ['is_creator' => true]);

    // the rest of your code.
}

And when a user want to 'join' an event:

app/Http/Controllers/SomeCoolController.php

public function join(JoinEventRequest $request)
{
    // Get the event
    $event = Event::find($request->event_id);
    // relate the ev
    auth()->user()->events()->attach($newEvent, ['is_creator' => false]);
    // or just this, because its already set to false by default:
    // auth()->user()->events()->attach($newEvent);

    // the rest of your code.
}