I am working on the Laravel back-end and I have a some question. Can I put some logic methods to the the models? Get data in specific format, for example. And some like this:
public function getAllWithObjects()
{
$all_photos = [];
$all_readed = $this::all()->toArray();
foreach($all_readed as $photo)
{
$all_photos[$photo["id_object"]][] = env("APP_URL")."/api/photos/".$photo["id"];
}
return $all_photos;
}
Or it’s better put to the controller?
Looked for some videos and nothing else.
2
Answers
Instead of doing a foreach statement to include the path of the image, you can actually insert it on the query using selectRaw and concat to joint the database field with a fixed stringg
I would suggest creating an attribute to get the object id for each model
and now your
AppModelsPhoto
has an object_id property,$photo->object_id