Yada
Yada

Reputation: 31263

Eloquent belongsTo relationships

Not sure why it's now working for me.

I have two tables

events
- id

selections
- id
- event_id

From my Event model I want to relationship selections

class Event extends Model
{
   ...
   public function selections()
   {
        return $this->belongsTo(Selection::class, 'event_id', 'id');
   }
}

My problem is that $event->selections relationship is not working. Keep getting null back.

Upvotes: 1

Views: 45

Answers (1)

Sanzeeb Aryal
Sanzeeb Aryal

Reputation: 3266

With the database schema, it is hasMany relation instead.

public function selections()
   {
        return $this->hasMany(Selection::class, 'event_id', 'id');
   }

See One to many relationship in documentation.

Upvotes: 2

Related Questions