php - How can i get query result id in controller from Laravel? -
i want take houses lists , houses pictures on mysql laravel. but, have problem. using process below:
$houses = houses::query() ->orderby('sort', 'asc') ->take('6') ->get();
this query, give houses list me. , need use houses retrieve photos, best think of:
$pictures = housephotos::query() ->where('house_id', '=', $houses->house_id) ->get();
question one: process correct?
question 2 : how can this?
add relation houses:
class houses extends model { public function photos() { return $this->hasmany(housephotos::class); } }
and can photos like:
$houses = houses::query() ->orderby('sort', 'asc') ->take('6') ->get(); foreach ($houses->photos $photo) { echo $photo->id; }
the problem is, select query run n times (5 times example).
if add with
query however, laravel run single in
query pictures:
$houses = houses::query() ->with('pictures') ->orderby('sort', 'asc') ->take('6') ->get();
Comments
Post a Comment