Support the ongoing development of Laravel.io →
Requests Input Installation
Last updated 1 year ago.
0

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.

0

My routes look like this:

Route::resource('articles', 'articleController');
Route::resource('comments', 'commentController');

I have a few questions:

  1. Is it a good practice to use a hidden form to add something to $request. It could be rewritten, right?
  2. Since I use my routes, aritcle's URL looks like '/articles/2'. Is it possible to use url facade to get previous URL and get an article id from the string? But there is still a problem with that. I can use human readable URL and there will be no id...
  3. About 'article.comment' in routes. In this situation to show all the articles, my URL should be like this: '/article/{article}/comment' where {article} is an id. But I want to show all the articles. Why do I need to put an id into URL?
0

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.

0

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?

0

I figured out :)

Need to do this in store method:

public function store(Request $request, $articleId)
    {
        ...
    }

Thank you for help!

Last updated 8 years ago.
0

Sign in to participate in this thread!

Eventy

Your banner here too?

Temoxa90 temoxa90 Joined 16 Nov 2015

Moderators

We'd like to thank these amazing companies for supporting us

Your logo here?

Laravel.io

The Laravel portal for problem solving, knowledge sharing and community building.

© 2024 Laravel.io - All rights reserved.