Get selected value from drop down Laravel 4 - php

I want to get the selected value from a drop down list in a controller method.
The drop down list :
{{ Form::select('Organization', $organization_list ,Input::old('Organization'), array('class' => 'form-control')) }}
$organization_list is an array of organizations..
I have tried to catch the selected item of this drop down list in a controller as follows:
Input::get('Organization');
But it gives me the array index instead of the real organization name..But the real organization name is shown in drop down list.. does anyone have a solution?

Pass an associative array To Form::select as the second argument
$organization_list = array_combine($organization_list, $organization_list); //Copies the array values to keys
{{ Form::select('Organization', $organization_list ,Input::old('Organization'), array('class' => 'form-control')) }}
http://php.net/array-combine

Related

Laravel Form Class without second argument

I simply want to give the text element of my edit-form a class.
{{ Form::text('first_name') }}
I know that i have to use an array with the class of the field as third argument. But i do NOT want to give a second argument. This one
{{ Form::text('first_name' , ' ' , array('class' => 'form-control')) }}
applys the class correctly, but sets the dafault value to an empty string. This is a problem, because now laravels auto-complete function doesn't fill the form with the correct data from the database. Is there a way to give no second argument or setting it to default like this?
{{ Form::text('first_name' , default , array('class' => 'form-control')) }}
Thanks!
Okay, i solved it myself. It is as easy as it always is. Simply use
{{ Form::text('first_name' , null , array('class' => 'form-control')) }}

Form dropdown menu passing array as object

I'm trying to pass trough form dropdown selection but I can't pass array(the rest of the form is using objects. This is my code
Controller
$var->user = $request->users->id;
view
{!! Form::select('users', $users,null, ['placeholder' => 'Pick a user']) !!}
One solution is converting array to object using eloquent, how can that be done
If you want to get selected user's ID, I guess you need to do this:
$request->users
You're getting "Trying to get property of non-object" error, because $request->users is not an object.
If you want to get the user list you can follow this:
In Controller:
$user_id = UserModel::lists('username','id')->all();
"usename" and "id" is user-table fields.you can replace any other fields which you will show in user list.
In view:
{!! Form::select('user_id', $user_id,Input::old('user_id'),['placeholder'=>'select user']) !!}

Laravel Select Box

I'm trying to figure out how I can structure my Laravel select menu so that it shows up as this for a final render. Has anyone done such a thing.
The location is a property of the arena object.
<option value="arena_id">Arena Name - Location</option>
{{ Form::select('arena_id', [ null => 'Please Select'] + $arenas, null, ['id' => 'arena_id']) }}
I asssume $arenas comes from something like Arena::where('foo', bar)->get(), but with get() you will get an instance of Illuminate\Database\Eloquent\Collection instead of an actual array which is what you want in Form::select.
So what you need to do is to use lists($field, $key), it will fetch you rows and return it as an array.
$arenas = Arena::where('foo', bar)->lists('name', 'id');
There is a code example here with some comments from users if you want to learn more.
You can use pluck function for getting the results as array
refer https://laravel.com/docs/5.1/collections#method-pluck
$select = $this->all()->pluck('title', 'id');
Then you can use below sample code for creating select box with selected option in blade template
{{ Form::select('name',$select,'selected option id',['class' => 'form-control']) }}

Laravel 4 display list of data into form selection

How do I display a list of books in laravel form builder selection?
BookController.php
$book_names = Book::all();
return View::make('books')->with('book_names', $book_names);
At the moment I only know how do manually input data:
{{ Form::select('book_name', array(
'book1' => 'book1',
'book2' => 'book2',
'book3' => 'book3')
}}
I want to do something like this:
{{ Form::select('book_name', array(
#foreach($book_names as $book_name)
$book_name->name => $book_name->name,
#endforeach
}}
But obviously It won't work..
Meet the lists() method. It allows you to create an array from one or two (key and value) properties of a collection:
$book_names = Book::lists('name');
return View::make('books')->with('book_names', $book_names);
And then simply pass that array:
{{ Form::select('book_name', $book_names) }}

How to input a variable value in field of name or id using blade template in laravel?

I need to input text name and id dynamically and I am trying as below
{{ Form::text('practice', $practice->name, array('class' => 'large-2','id'=<?php echo $inputTextName;?>)) }}
But it is not parsing PHP here. Even I tried 'id'={{$inputTextName}} but it is giving error.
Can you try this,
{{ Form::text('practice', $practice->name, array('class' => 'large-2','id'=>$inputTextName)) }}

Categories