2

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?

asked Apr 14, 2016 at 19:30

1 Answer 1

1

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))) }}
answered Apr 14, 2016 at 19:41
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.