I am trying to work out how to convert the result of Laravel's DB::raw to an array of strings, similar to ExecuteScalar in other languages but for many rows?
In my resource, I call the method like:
return [
'id' => $this->id,
'name' => $this->name,
'member_images' => $this->getNewestMemberPictures(),
'created_at' => $this->created_at,
'updated_at' => $this->updated_at,
];
And then make a call to DB::raw for the member images:
public function getNewestMemberPictures() {
return DB::raw("SELECT `picture`
FROM `scraper_profile_data` pd
INNER JOIN `scraper_collection_entries` ce ON ce.`item_id` = pd.`profile_id`
WHERE ce.`collection_id` = " . $this->id .
"ORDER BY ce.`created_at` DESC
LIMIT 10;");
}
Currently member_images is set to empty, I can only assume it is because Laravel doesn't automatically cast it to a string of arrays. How can I do this with Laravel?
DB::rawwraps an expression in an object to signify it will be used raw in a query, it returns anIlluminate\Database\Query\Expressionobject ... if you want to execute a raw select statement to get a result you are probably looking forDB::select(...)getNewestMemberPictures, do I have to explicitly foreach and call->picture? There is nothing in their documentation for returning an array of column values, that I can see.