Reputation: 2041
I'm trying to create a form in laravel to update some data. I started off by using the following in my blade template to fill the form with data from a model:
{{ Form::model($sensor, array('action' => 'settingsController@editSensor', $sensor->s_id)) }}
$sensor is send to the view from the controller in this way:
return view('settings.sensors.edit')->with('sensor', App\Sensor::find($sensorId))
The documentation isn't to clear about whether or not I also need to use Form::open , but the form open tag was already created, so I figured this would be enough.
In my routes I have these routes:
Route::get('/settings/sensors/edit/{sensorId}',['as' => 'sensor.edit', 'uses' => 'settingsController@editSensor']);
Route::post('/settings/sensors/edit/{sensorId}',['as' => 'sensor.edit', 'uses' => 'settingsController@editSensor']);
I could access the form by going to a url like http://localhost:8000/settings/sensors/edit/105 , the prefilling with data from the model worked like a charm.
The problem I had was that the submit button did now work. It would submit to http://localhost:8000/settings/sensors/edit/%7BsensorId%7D Clearly, the sensorId parameter was not being substituded properly.
I rewrote the Form::model function call after some googling to this:
{{ Form::model($sensor, array('route' => route('sensor.edit',$sensor->s_id)), $sensor->s_id) }}
When I now open the page, I directly get this error: Route [http://localhost:8000/settings/sensors/edit/105] not defined
That seems odd, since I made no change to the routes, and there is a route defined for this url. Any idea where I'm going wrong?
Upvotes: 2
Views: 1436
Reputation: 25374
You're essentially duplicating the route call. When you do Form::model($sensor, array('route' =>
, Laravel already knows that you're trying to access a named route, and is expecting a route name. So when you then do route(...)
, it first converts that route call before trying to look for a route named that.
In other words, it's not complaining that the page http://localhost:8000/settings/sensors/edit/105
does not exist - it's complaining that no route has that as its as
name.
Remove it, and replace it by an array with the route name and the parameter instead. Then you can also get rid of the extra $sensor->s_id
, like this:
{{ Form::model($sensor, array('route' => array('sensor.edit', $sensor->s_id))) }}
Upvotes: 1