It would work that.
Possible way to solve this:
You need to specify the route for this action: Route::post('/comments/store/{article}', [ 'uses' => 'CommentController@store' ]);
You need to register model for a router (in RouteServiceProvider, boot method): $router->model('article', 'App\Article'); // or whenever it is defined.
In store() method will you receive instance of the article, nor ID of it but whole object.
Hope this will help.
Hi androschukandriy!
Well, actually I've registered the route Model Binding with the "Article" Model.
I finally could solve the problem but I'm not sure if It's the best/safest way... I passed the variable through the Form with a "hidden" input.
This is the code if sometime can help:
The Form code: (It's from the show article view and it has the "$article" variable)
<h5><strong>Add a comment:</strong></h5>
{!! Form::open(['url' => 'comments' ]) !!}
{!! Form::hidden('article_id', $article->id) !!}
@include('comments.form')
{!! Form::close() !!}
This is the controller's code:
public function store(CommentRequest $request)
{
Comment::create($request->all());
flash()->success('Comment Added!');
return redirect()->back();
}
And finally the route:
Route::resource('comments', 'CommentController');
You didn't specify that the URL has to accept article_id
.
Please see the route I've meant.
Sign in to participate in this thread!
The Laravel portal for problem solving, knowledge sharing and community building.
The community