25.06.2018 Views

Full-Stack Vue.js 2 and Laravel 5

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Image links<br />

The payload for each listing in the web service should include links to these new<br />

images so a client app knows where to find them. Let's add the image paths to<br />

our listing API payload so it looks like this: { "id": 1, "title": "...", "description":<br />

"...", ... "image_1": "http://vuebnb.test/app/image/1/Image_1.jpg", "image_2":<br />

"http://vuebnb.test/app/image/1/Image_2.jpg", "image_3":<br />

"http://vuebnb.test/app/image/1/Image_3.jpg", "image_4":<br />

"http://vuebnb.test/app/image/1/Image_4.jpg" }<br />

The thumbnail image won't be used until later in the project.<br />

To implement this, we'll use our model's toArray method to make an array<br />

representation of the model. We'll then easily be able to add new fields. Each<br />

mock listing has exactly four images, numbered 1 to 4, so we can use a for loop<br />

<strong>and</strong> the asset helper to generate fully-qualified URLs to files in the public folder.<br />

We finish by creating an instance of the Response class by calling the response<br />

helper. We use the <strong>js</strong>on; method <strong>and</strong> pass in our array of fields, returning the<br />

result.<br />

app/Http/Controllers/ListingController.php: public function get_listing_api(Listing<br />

$listing) { $model = $listing->toArray(); for($i = 1; $i id . '/Image_' . $i . '.jpg'<br />

); } return response()-><strong>js</strong>on($model); }<br />

The /api/listing/{listing} endpoint is now ready for consumption by a client app.

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

Saved successfully!

Ooh no, something went wrong!