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']) }}
Related
I've got a problem in Laravel. I have passed my whole table to my view like this from my controller:
$usersTable = DB::table('users')->get();
return view('users')->with('users', $usersTable);
In a foreach loop I can perfectly get each of the values like this in my view:
#foreach($users as $user => $value)
<div class="projectBox">
<br><span class="projectBoxName">{{ $value->name }}</span>
#php
echo Form::image('/images/edit.png', "",['class' => "editUserBtn", 'userId' => $value->id]);
#endphp
<br><span class="projectBoxSmallText projectBoxEmail">{{ $value->email }}</span>
<br><span class="projectBoxSmallText projectBoxId">ID: {{ $value->id }}</span>
<br><span class="projectBoxSmallText projectBoxProjects">Currently no projects</span>
</div>
#endforeach
But I also need to access these values outside my foreach loop, how can I do that?
echo Form::text('email', "$users->$value->email", array('placeholder' => "Email"));
Ain't working...
This gives my the whole object in this form
[{"id":"1","name":"Administrator","email":"admin#mail.com","password":"$2y$10$Re3Ahf.SwU5vj4UvtU5Dy.jxaZMsUNC2WhuJMwsNy9gu6TST4PuRG","remember_token":null}]
How to get only the email? I also tried using indexes, but those weren't working.
Thanks!
Edit:
Full situation:
I have a list of users with their extra information (mail, tel,...). In those user-boxes there is a button which says 'edit user' when I click that a modal opens giving the current mail and tel. So I can't say in my controller WHO's mailaddress to return because I only know that at the moment the user clicks a client-side button.
Images: http://imgur.com/a/krDrY
(Edit button is that small circle with three dots).
To access a collection without using loop, you should use collection methods:
$users = User::get();
$users->where('name', 'John Smith')->first()->email
This will not create any additional queries since you've already eager loaded data.
If you want to load just one user, use first() instead of get():
$users = User::first();
If you'll use get() and then [0] or first() like some guys recommend here, you'll load all users data into the memory for each request which will overload your server.
Also, using indexes to access data (like $users[4]['email']) is a bad practice. Avoid it if possible.
You need to add as first element by adding [0]
echo Form::text('email', $users[0]->email, array('placeholder' => "Email"));
I suggest use ->first() instead of ->get() to get single object. And remove loop and use anywhere you want.
You are using a collection of users to get the first user's email, you do
$users->first()->email;
Looks like it's coming in as JSON. Try json_decode($data) and $data->email to get that attribute.
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']) !!}
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
I got value from database and then i need to set those value to textbox. I have created a controller file with the method name of edit look like below
userdata.blade.php:
public function edit($id)
{
echo "You have clicked edit link".$name;
$editdata = DB::table('newuser')->where('Id','=',$id)->get();
return View::make('editdata',array('list' => $editdata));
}
I have passed array of value as parameter to the view file.now i need to diaplay the value of name to textbox.how to do that in html page using laravel. My html page look like below
editdata.blade.php:
<html>
<head></head>
<body>
<div>
{{Form::open(array('url' => 'login', 'method' => 'post'))}}
{{Form::label('name','Name',array('id'=>'label-name'))}}
{{Form::text('name',{{$list->Name}}}}
{{ Form::close() }}
</div>
</body>
</html>
can anyone tell me that what mistake i did.Thanks in advance
Just remove the curly brackets, you are already "inside" PHP code and don't need them:
{{ Form::text('name',$list->Name) }}
Also you get a collection from your controller you probably want to do:
$editdata = DB::table('newuser')->where('Id','=',$id)->first();
Or even:
$editdata = DB::table('newuser')->find($id);
get() returns a collection (multiple rows) and not the model itself. You can use User::find($id) which gives you direct access to the model with the specified Id.
When not using eloquent just replace get() with first()
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) }}