0

Our Application has 3 Model "Notification", "Postlike" and "Post"

In Notification Model:

public function postlike()
{
    return $this->belongsTo('App\Models\PostLikes', 'postlist');
}
public function post()
{
    return $this->hasMany('App\Models\PostLikes', '_id', 'c_post_id');
}

In Postlike Model:

public function postlist()
{
    return $this->hasMany('App\Models\Post', '_id', 'c_post_id');
}

In Notification Repository: (Query)

public function getnotification($userId)
{
     $notification = $this->makeModel()
     ->with('post') 
     ->with('postlike')
     ->with('notification')
      ->orderBy('created_at' , 'desc')
      ->where('replied_id', $userId)    
     ->get();

     return $notification;
}

For more information we have attached following Image enter image description here

2 Answers2

1

When i firt saw your post, i thought your problem could be solved using Laravel's built-in eager loading, so it lets you do something like this:

$notification = $this->makeModel()
     ->with('post', 'postlike', 'postlike.postlist')
     //...
     ->get();

If thats your case, these links shoud do the trick:

https://laracasts.com/discuss/channels/eloquent/eager-load-multiple-nested-relationships

laravel eloquent eager load multiple nested relationships

Im not sure, but it seems a little bit odd to me, that 'Notification' Model has two relations with 'Postlike' model. Maybe you should consider using some pivot table.

Hope it helps,

Renato Gomes
  • 126
  • 4
0

Notification Model :

public function postlike()
{
    return $this->belongsTo('App\Models\PostLikes', 'c_post_id');
}

Query:

$notification = $this->makeModel()
{
    ->with('post', 'postlike', 'postlike.postlist')->get()
}