Created
March 14, 2016 02:03
-
-
Save jackfruh/a87568188581fdf8ac1d to your computer and use it in GitHub Desktop.
Using Select dropdowns with Laravel Form Model Binding LV5
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{!! Form::model($event, array('route' => array('admin.event.update', $event->id), 'method' =>'PATCH')) !!} | |
<div class="form-group"> | |
{!! Form::label('organization_name', 'Organization') !!} | |
{!! Form::select('organization_name', $organizations, $event->organization_id) !!} | |
<!-- note above, the select takes the array we made in the controller ($organizations) and it also takes the field from the model that matches | |
(In this case $event->organization_id) | |
from this we can expect: | |
- that we have an event table with a field for the organization ID. | |
- that on the our form, when viewed in a browser, we'll see a drop down with the NAMES of the organizations from the organization table | |
- that when we select an organization by NAME from the select dropdown in the browser, | |
it will actually be the ID from the organization table (Organization->id), | |
that gets stored in the EVENT table in the Organization_id field (Event->Organization_id) | |
Hope this makes sense! | |
PS thanks to Samantha Getz for helping me with this at LaraconUS 2015 | |
- Jack | |
--> | |
</div> | |
{!! Form::submit('Submit', array('class'=>'btn btn-primary form-control')) !!} | |
{!! Form::close() !!} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php namespace Blah Blah Blah (this would be your namespace here); | |
use App\Models\Event; | |
use App\Models\Organization; | |
use OTHER STUFF HERE; | |
class EventController extends Controller { | |
public function edit($id) | |
{ | |
/* | |
Whats happening below.. | |
We're going to open a view (edit.blade.php in the views\admin\events folder) | |
We're going to pass it two variables, | |
The first is called 'event' and it will contain an event object (thats what the event::find($id) does) | |
The second is called 'organizations' and it will contain an Array. (not an object, this is important) | |
now the array will have every record in the Organiztion table and it will have two fields from the DB | |
- organiaztion_name | |
- id | |
these will be used by the form | |
*/ | |
return view('admin.events.edit')->with('event', event::find($id)) | |
->with('organizations', Organization::lists('organization_name', 'id')); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment