I have a model called Members, another called Awards and another called Events.
Each member can have multiple awards more than once and each award can be assigned to multiple members, these can be awarded on the members profile or as a result of an event.
My member model relationship:
public function Awards()
{
return $this->belongsToMany('App\Award', 'award_members', 'member_id', 'award_id')
->withTimestamps()
->orderBy('award_members.created_at','desc');
}
My relations table:
Schema::create('award_members', function (Blueprint $table) {
$table->increments('id');
$table->integer('user_id');
$table->integer('club_id');
$table->integer('award_id');
$table->integer('member_id');
$table->integer('event_id')->nullable();
$table->timestamps();
});
My view output:
@foreach($member->Awards as $a)
{{ $a->title }} | {{ $a->pivot->created_at }}
@endforeach
This outputs:
- Award 1 | 12/08/2016
- Award 2 | 12/08/2016
- Award 1 | 11/08/2016
I'd also like to output if stored in the relations table the event name if it was added to the members profile via an event. the above only shows details of awards when added directly to the member.
In my relations table an entry from an event would look similar to:
- id = 4
- user_id = 1 (related to user table)
- club_id = 1 (related to club table)
- award_id = 3 (related to awards table)
- member_id = 1 (related to members table)
- event_id = 1 (related to events table but can be NULL)
How can I output when entered the event name?