I am trying to get this from a query using eloquent.
$data = [
"user" => [
"img_profile" => "profileimage",
"username" => "myusername"
],
"description" => "postdescription",
"img" => "postimg"
];
I managed to get this using the following php code, but I want to get this from the query, is there any way?
$posts = posts::join('business', 'posts.user_id', '=', 'business.user_id')
->join('cities', 'business.city_id', '=', 'cities.id')
->select(
'posts.description as description',
'posts.img as img',
'business.name as name',
'business.img_profile as img_profile',
'business.username as username'
)
->where('business.city_id', $city)
->inRandomOrder()
->limit('10')
->get();
foreach($posts as $post){
$data[$i] = [
"user" => [
"username" => $post->username,
"img_profile" => $post->img_profile
],
"description" => $post->description,
"img" => $post->img
];
$i++;
}
$posts?businesstable, rather something likePost::with("business", "city"). Eloquent can do this, but you're not going to be able to do what you want with just query builder.