Answers for "what is a resource in laravel"

7

laravel create resource controller

php artisan make:controller PhotoController --resource --model=Photo
Posted by: Guest on April-23-2020
0

resources laravel

return [
            'data' => $this->collection,
            'meta' => ['song_count' => $this->collection->count()],
        ];

{
  "data": [  ],
  "meta": {
    "song_count": 3
  }
}

    return [
        'id' => $this->id,
        'title' => $this->title,
        'rating' => $this->rating,
    ];
    return [
            'data' => SongResource::collection($this->collection),
            'meta' => ['song_count' => $this->collection->count()]
        ];

{
  "data": {
    "id": 1,
    "title": "Mouse.",
    "rating": 3
  },
  "meta": {
    "anything": "Some Value"
  }
}

return [
            'type' => 'articles',
            'id' => (string) $this->id,
            'attributes' => [
                'id' => (string) $this->id,
                'title' => $this->title,
                'slug' => $this->slug,
                'body' => $this->body, //\Str::words($this->body, 87, ''),
                'votes' => $this->voters()->count(),
                'upvotes' => $this->up ?? $this->upVoters()->count(),
                'downvotes' => $this->down ?? $this->downVoters()->count(),
                'views' => $this->views_count,
                'created_at' => $this->created_at,
                'is_up_voted' => auth()->guard('api')->id() ? auth()->guard('api')->user()->hasUpVoted($this->setAppends([])) : false,
                'is_down_voted' => auth()->guard('api')->id() ? auth()->guard('api')->user()->hasDownVoted($this->setAppends([]))
                    : false,
                'real' => $this->num,
            ],
            'relationships' => new ArticleRelationshipResource($this),
            'links' => [
                'self' => route('articles.show', ['article_json' => $this->id]),
            ],
        ];
Posted by: Guest on October-14-2021

Code answers related to "what is a resource in laravel"

Code answers related to "Shell/Bash"

Browse Popular Code Answers by Language