Skip to content

Instantly share code, notes, and snippets.

@webber12
Last active October 17, 2022 21:12
Show Gist options
  • Save webber12/42233b9c7ed60cc01f74df5edc1c4648 to your computer and use it in GitHub Desktop.
Save webber12/42233b9c7ed60cc01f74df5edc1c4648 to your computer and use it in GitHub Desktop.
User List with latest item
model User
```
public function latestItem()
{
return $this->hasOne(Item::class)->latestOfMany();
}
```
model Item
```
protected $appends = [ 'my_date' ];
protected function getMyDateAttribute()
{
return \Carbon\Carbon::parse($this->created_at)->format("d.m.Y H:i");
}
```
Route Controller Method
```
public function users()
{
$items = User::with(['latestItem'])->whereIn('id', [4, 11])->get()->toArray();
dd($items);
}
```
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment