I have some controllers and resources for different models like users and blog posts. Every endpoint for my user model is working properly. I modeled my blog post model, controller, and resources similarly but the blog post model does not work when requesting specific posts. Blog post methods like index()
and store()
are working properly however.
api.php
Route::prefix('v1')->group(function (){ Route::apiResource('/blog', 'App\Http\Controllers\API\v1\BlogPostController'); }
BlogPostController.php
/** * @param BlogPost $blog_post * @return BlogPostResource */ public function show(BlogPost $blog_post) : BlogPostResource { return new BlogPostResource($blog_post); } /** * @return BlogPostResourceCollection */ public function index() : BlogPostResourceCollection { return new BlogPostResourceCollection(BlogPost::paginate()); }
The BlogPostResource()
is a default resource created with artisan.
I am targeting .../v1/blog/2
which is an existing blog post id
.
When I dd()
on $blog_post
in show()
I get this as one of the returned fields +exists: false
.
I must be missing something.
Edit: It may be worth noting that my user model has this method:
public function blogPosts() { return $this->hasMany(BlogPost::class); }
...and my blog post model has this method:
public function author() { $this->belongsTo(User::class); }
Edit: I thought it may have been something wrong with my route/model binding but, again, the index()
and store()
methods are working fine with the route.
The only methods that don't work are those that accept a BlogPost
as an argument.
Using the index()
method endpoint proves our blog post with id
of 2
exists
{ "id": 2, "created_at": "2021-01-21T01:29:36.000000Z", "updated_at": "2021-01-21T01:29:36.000000Z", "user_id": 52, "title": "quae porro laborum incidunt vel", "body": "Molestiae eius debitis..." },
https://stackoverflow.com/questions/65837061/laravel-8-api-show-update-destroy January 22, 2021 at 07:06AM
没有评论:
发表评论