Support the ongoing development of Laravel.io →
Requests Eloquent Forms
Last updated 2 years ago.
0

It would work that.

Possible way to solve this:

  1. You need to specify the route for this action: Route::post('/comments/store/{article}', [ 'uses' => 'CommentController@store' ]);

  2. You need to register model for a router (in RouteServiceProvider, boot method): $router->model('article', 'App\Article'); // or whenever it is defined.

  3. In store() method will you receive instance of the article, nor ID of it but whole object.

Hope this will help.

0

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');
Last updated 9 years ago.
0

You didn't specify that the URL has to accept article_id. Please see the route I've meant.

0

Sign in to participate in this thread!

Eventy

Your banner here too?

Polinicles polinicles Joined 26 Nov 2014

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.