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.

Adding the thumbnail<br />

Each mock listing has a thumbnail version of the first image, which can be used<br />

for the listing summary. The thumbnail is much smaller than the image we use<br />

for the header of the listing page <strong>and</strong> is ideal for the listing summaries on the<br />

home page. The URL for the thumbnail is public/images/{x}/Image_1_thumb.jpg where<br />

{x} is the ID of the listing.<br />

Collection objects have a helper method, transform, that we can use to add the<br />

thumbnail image URL to each listing. transform accepts a callback closure<br />

function that is called once per item, allowing you to modify that item <strong>and</strong> return<br />

it to the collection without fuss.<br />

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

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

>transform(function($listing) { $listing->thumb = asset( 'images/' . $listing->id .<br />

'/Image_1_thumb.jpg' ); return $listing; }); $data = collect(['listings' =><br />

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

/*<br />

[<br />

"listings" => [<br />

0 => [<br />

"id" => 1,<br />

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

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

"price_per_night" => "...",

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

Saved successfully!

Ooh no, something went wrong!