Sending slug in route using post in form

1.3k views Asked by At

I am trying to send {slug} in route:

Route::post('page-edit/{slug}', 'PageController@postSavePage');

in view I have:

{!! Form::open(array('action' => 'PageController@postSavePage')) !!}

in controller:

public function postSavePage($slug = null){
dd($slug);
}

but I have error:

Missing required parameters for [Route: ] [URI: page-edit/{slug}]. (View: /var/www/html/CMS/resources/views/admin/pages/page-edit.blade.php)

What is the correct syntax?

2

There are 2 answers

3
Jerodev On BEST ANSWER

If you want the slug to be optional, you have to add a question mark (?) to the parameter name in the routes.php file.

Like so:

Route::post('page-edit/{slug?}', 'PageController@postSavePage');

If you do not do this, you have to add the slug to the url of your form. Like so:

{!! Form::open(array('action' => array('PageController@postSavePage', 'slug'))) !!}

Update:

I suppose the page you are sending this request from is the same url as the one you are trying to post to. In this case the best thing to do would be to leave the action field of your form empty. This will make sure the form is submitted to the same url.

So you can just do this:

{!! Form::open() !!}
2
Nutshell On

I think you're missing sending the {slug} parameter in the blade template as well :

{!! Form::open(array('action' => 'PageController@postSavePage', $slug))) !!}

In your controller :

public function postSavePage($slug = null){
  // here's you can define $slug var, for example : 
  $slug = 3;
  return view('admin.pages.page-edit', compact('slug'));
}