Use Twill as Headless CMS
April 24, 2020 at 9:03amUse Twill as Headless CMS
April 24, 2020 at 9:03am (Edited 2 years ago)Hello, ive made Twill working as an API Solution.
The only problem is that Images and Files are not beeing sent to the API route.
Every Field is there but not the image or file itself.
Does someone know how I can get the Images in my Controller and pass them to an API Call?
Here is a Screenshot of the Response.
Same don't working with normal Images without Blocks. I don't get any Image.

Thank you in Advance
April 24, 2020 at 9:18am
I'd suggest working with Eloquent API Resources. Using those, you can 1) choose which data keys are included and 2) include additional data that isn't part of the model's JSON representation (like the image src in your example). It's a great way to also reduce payload and speed up your responses by only exporting the data you'll actually need.
Thanks!
I got it somehow to work but its not straightforward as you told me.
My ChallengeController show method looks like this
public function show(Challenge $challenge)
{
return new ChallengeResource($challenge);}
ChallangeRessource looks like this:
class ChallengeResource extends JsonResource
{
/**
* Transform the resource into an array.** @param \Illuminate\Http\Request $request* @return array*/public function toArray($request){return ['id' => $this->id,'title' => $this->title,'cover' => $this->cover,'description' => $this->description,'prize' => $this->prize,'duration' => $this->duration,'teams_max' => $this->teams_max,'company_name' => $this->company_name,'company_description' => $this->company_description,];return parent::toArray($request);}
}
How you would achieve to get the necessary Blocks and Medias to also return them somehow.
April 24, 2020 at 8:42pm
That's the simple version anyway. I usually create a
MediaResource
for medias and a BlockResource
for each block type (TextBlockResource
, SlideshowBlockResource
, ...), but that might be too much overhead depending on the complexity of your API.April 28, 2020 at 10:46am
The Problem is that if I put in your code in my ChallengeRessource .php than it trows me an error
Call to a member function first() on null even there is a image with the filed "cover" and correct saved in database and in backend
And blocks are "null" its strange. Did I forgot something?
<?phpnamespace App\Http\Resources;use Illuminate\Http\Resources\Json\JsonResource;use A17\Twill\Models\Behaviors\HasMedias;use A17\Twill\Models\Behaviors\HasBlocks;class ChallengeResource extends JsonResource{use HasMedias, HasBlocks;/*** Transform the resource into an array.** @param \Illuminate\Http\Request $request* @return array*/public function toArray($request){return ['id' => $this->id,'name' => $this->name,// 'cover' => $this->cover,'description' => $this->description,'prize' => $this->prize,'duration' => $this->duration,'teams_max' => $this->teams_max,'company_name' => $this->company_name,'company_description' => $this->company_description,'cover' => $this->imageObject('cover'),// // 'images' => $this->imageObjects('images'),'blocks' => $this->blocks,];return parent::toArray($request);}}