Last active
October 17, 2022 21:12
-
-
Save webber12/42233b9c7ed60cc01f74df5edc1c4648 to your computer and use it in GitHub Desktop.
User List with latest item
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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