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

Popular posts from this blog

node.js - Node js - Trying to send POST request, but it is not loading javascript content -

javascript - Replicate keyboard event with html button -

javascript - Web audio api 5.1 surround example not working in firefox -