My example:
$photo = Photo::with([
'owner' => function ($query) {
$query->select(['id', 'nick_name']);
}
])
->where('fid', $photoID)
->select(['fid', 'member_id', 'filename', 'album_id', 'description', 'added_timestamp'])
->firstOrFail();
// Convert created date timestamp to a readable time
$photo->added_timestamp = $this->unixTimestampToTime($photo->added_timestamp);
It works, but can i have $photo->added_timestamp already changed by some method in eloquent?
Something like accessor method in model, but it's not good because i need change this attribute in each controller method differently.
Maybe it exists some cleaner way for this
If you need to change the attribute in each controller method differently, then I would probably stick with the accessor method in the model, since you will need to convert added_timestamp
to the format to whatever you want. Then you could convert the added_timestamp
attribute based on a parameter sent to the accessor method.
Sign in to participate in this thread!
The Laravel portal for problem solving, knowledge sharing and community building.
The community