My database schema:
user:
- id
- email
...
notification_user:
- user_id
- notification_id
notifications:
- id
- channel_id
- message
...
channels:
- id
- name
...
User Model:
public function notifications()
{
return $this->belongsToMany('App\Notification');
}
Notification Model:
public function users()
{
return $this->belongsToMany('App\User');
}
What is the problem:
App\User::find(1)->notifications
shows all notifications which belongs to this user and it works fine, but I also need to include data from channels
table.
<App\Notification #000000004e6906c1000000014ed0d97c> {
id: 20,
channel_id: 3,
message: "...",
created_at: "2015-05-31 17:37:12",
updated_at: "2015-06-07 10:37:12",
pivot: <Illuminate\Database\Eloquent\Relations\Pivot #000000004e690718000000014ed0d97c> {
user_id: 1,
notification_id: 20
}
},
The above is output from tinker. Question is: How to include data from channels
table for every notification.
Add a
Channel
relationship toNotification
model.Then you can load the notifications with channel like below.