25.06.2018 Views

Full-Stack Vue.js 2 and Laravel 5

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Home page initial state<br />

Using Eloquent ORM, it's trivial to retrieve all our listing entries using the<br />

method Listing::all. Multiple Model instances are returned by this method within a<br />

Collection object.<br />

Note that we don't need all the fields on the model, for example, amenities, about,<br />

<strong>and</strong> so on are not used in the listing summaries that populate the home page. To<br />

ensure our data is as lean as possible, we can pass an array of fields to the<br />

Listing::all method that will tell the database to only include those fields<br />

explicitly mentioned.<br />

app/Http/Controllers/ListingController.php: public function get_home_web()<br />

{ $collection = Listing::all([ 'id', 'address', 'title', 'price_per_night' ]); $data =<br />

collect(['listings' => $collection->toArray()]); return view('app', ['data' =><br />

$data]); }<br />

/*<br />

[<br />

"listings" => [<br />

0 => [<br />

"id" => 1,<br />

"address" => "...",<br />

"title" => "...",<br />

"price_per_night" => "..."<br />

]

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!