Reputation: 23
I want to create API from my plugin but API returns all I need expect image paths. Do you know anyone how to get image paths and return in my API?
In my routes.php I have
<?php
use Mycompany\ArubaPlaces\Models\Places;
Route::get('api/places', function(){
$places = Places::All();
return $places;
});
And this is my modelI have this relation. I need "placeimages" paths in my API:
public $attachMany = [
'placesimages' => 'System\Models\File'
];
fields.yaml
fields:
name:
label: Title
span: auto
required: 1
type: text
slug:
label: Slug
span: auto
preset:
field: name
type: slug
type: text
description:
label: Description
size: small
oc.commentPosition: ''
span: full
type: richeditor
latitude:
label: Latitude
span: auto
required: 1
type: text
longitude:
label: Longitude
span: auto
required: 1
type: text
placesimages:
label: 'Place images'
mode: image
useCaption: true
thumbOptions:
mode: crop
extension: auto
span: full
type: fileupload
active:
label: Aktívne
span: auto
type: switch
Upvotes: 1
Views: 490
Reputation: 3195
1) First option is adding protected $with = ['placesimages'];
to your Places
model to eager load the desired relationships, here you can add more than one relation by default.
2) You can do the eager loading in your query e.g Places::with('placesimages')->get();
if you do not want to eager load all files by default. Also here with
accepts an array of relations.
If you are sure that in your app everytime you query a place you want to fetch the images then go for option one, otherwise, option 2 will only get the images when only you specify it in the query builder.
Read more about eager loading
Upvotes: 1