Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

PHP

geoffrey
geoffrey
28,736 Points

LARAVEL - Form binding for SELECT menu

Hey there, I'm still discovering laravel, I try to build my own project and I'm having some difficulties for something else again.

My problem is I can't have a form binding with my select menu.

Here is the edit method of my deviceController.php

<?php

    public function edit($id)
    {
        $categories = CategoryDevice::lists('name','id');
        $selectedDevice = Device::findOrFail($id);
        return View::make('devices.edit')
        ->withDevice($selectedDevice)
        ->withCategories($categories);
      }

?>

With it when I click the update button of my selected device, the inputs have indeed the old values put when I had saved the field's value preivously, but not the select menu. It's reinitialized.

I can't retrieve automatically the selected value set in the database for a particular device.

Any idea on the way to achieve it ?

I'm stuck !

1 Answer

geoffrey
geoffrey
28,736 Points

Well, quite odd as feeling, but I've been looking for an hour on the way to do that, and I've finally found the solution, just after I had posted here... I should post faster here to get my questions answered on my own quickly, this situation happened to me a lot lately lol.

In case someone would be interested, the matter here is not really the controller, at least not on my case.

My problem was the third parameter in Form::select inside my devices.edit.php page was set to null, so, everytime that was the default value displayed.

A quick explanation:

<?php

/*What I had as a start*/

 {{ Form::select('category',$categories,null,['class'=>'form-control'])}}

/*The good way to achieve this*/

{{ Form::select('category',$categories,$device->category_device_id,['class'=>'form-control'])}}


?>

null gets replaced by $device->category_device_id which is the right column inside my DB,