How are your routes structured? If you use a resource route the comment would be a nested route like this:
Route::resource('article.comment', ...)
That way all the comment methods would get an additional parameter article_id (since the routes would look like this: /article/15/comment with 15 being the article id).
If you don't have a resource controller you could either add an article_id to your URL (and then change the action attribute of the form) or add a hidden input article_id to the form.
My routes look like this:
Route::resource('articles', 'articleController');
Route::resource('comments', 'commentController');
I have a few questions:
Sorry, I wasn't clear enough. :) You'll actually have two routes:
Route::resource('article', ...
Route::resource('article.comment', ...
So your list of all articles would be /article, your list of all comments for one article would be /article/15/comment and to add a new comment for an article you would do a post request to /article/15/comment - that would call the store() method in your commentController which now has an articleId Parameter.
Still the articleId can be overwritten so you always should check (for example with FormRequests) if e.g. a certain user is allowed to post a comment to a certain article etc.
Oh, now I get it! But still have a problem :)
I can't find 'aricle_id' variable( I changed url in form to 'article/15/comment'). Should it be in $request?
I figured out :)
Need to do this in store method:
public function store(Request $request, $articleId)
{
...
}
Thank you for help!
Sign in to participate in this thread!
The Laravel portal for problem solving, knowledge sharing and community building.
The community