Need to get distinct row.
Is there any way in laravel eloquent to avoid the duplication of data. Need to get unique users after joining with image table.
Now, I'm getting the same users, need to avoid the repetition of the same user.
Model Image
public function user()
{
return $this->belongsTo('App\User');
}
User Table
id | name
---------
| 1| Joe
| 2| Ben
| 3| Don
Image Table
id | name | user_id
---------------------
| 1 | 1.png | 3
| 2 | 2.png | 1
| 3 | 3.png | 1
| 4 | 4.png | 2
join using eloquent :
Image::with('user')->orderBy('id', 'desc')->paginate(10);
Required Output
"data": [
{
"id": 4,
"name": "4.png",
"user_id": 2,
"user": {
"id": 2,
"name": "Ben",
}
},
{
"id": 3,
"name": "3.png",
"user_id": 1,
"user": {
"id": 1,
"name": "Joe",
}
},
{
"id": 1,
"name": "1.png",
"user_id": 3,
"user": {
"id": 3,
"name": "Don",
}
},
],