Reputation:
View:
{{ Form::open(array('url'=>'agents/searchStaff',
'class'=>'form-search',
'role'=>'form')) }}
<select class="form-control">
@foreach ($company->users as $user)
<option id="{{ $user->id }}" name="agent">
{{ $user->firstname }} {{ $user->surname }}
</option>
@endforeach
</select>
{{ Form::close() }}
Route:
Route::post('agents/searchStaff', 'AgentsController@postSearchStaff');
Controller:
public function postSearchStaff()
{
$agent_id = Input::get('agent');
dd($agent_id); // For testing purposes - Always returns NULL.
}
I'm unsure as to why the option value's ID isn't being passed onto the controller. Any help and guidance would be appreciated.
Upvotes: 2
Views: 2307
Reputation: 81187
You need to change id={{ $user->id }}
to value={{ $user->id }}
,
but this:
<select class="form-control">
@foreach ($company->users as $user)
<option id="{{ $user->id }}" name="agent">
{{ $user->firstname }} {{ $user->surname }}
</option>
@endforeach
</select>
can be done in one line with a bit sugar in your model:
// view
{{ Form::select('agent', $users, $selectedAgentId, $attributes) }}
// User model
public function getFullnameAttribute()
{
return $this->attributes['firstname'].' '.$this->attributes['surname'];
}
// controller:
$users = $company->users->lists('fullname','id'); // Collection method
Upvotes: 1
Reputation: 3205
Change
<select class="form-control">
@foreach ($company->users as $user)
<option id="{{ $user->id }}" name="agent">
{{ $user->firstname }} {{ $user->surname }}
</option>
@endforeach
</select>
to
<select class="form-control" name="agent">
@foreach ($company->users as $user)
<option value="{{ $user->id }}">
{{ $user->firstname }} {{ $user->surname }}
</option>
@endforeach
</select>
Upvotes: 0