Dropdown menu with eloquent - php

I have a form and I want to give user ability to choose user in a drop down, but when I return the data it gives me an object, How can I make a drop down select for each user in array.
This is my code
view
{!! Form::select('users', array($users),null, ['placeholder' => 'Pick a user']) !!}
controller
$users = User::lists('name');
return view('view')->with('users', $users);
now it returns
Placeholder
["user1", "user2"]

You need to add ID to the list to make it work:
$users = User::pluck('name', 'id');
Also, use pluck() instead of lists() because lists() is depricated.

Related

Fetch the value of the data using Select Dropdown after Update Laravel

I cannot fetch the value of the data after I store or created it .. I want to fetch the data in edit.blade.php after I created it. I used the old function method but it didnt work
My Controller
public function edit($id)
{
$movieSubs = MovieSubtitle::find($id);
$movieTitle = Movie::all()->pluck('title','id');
return view('admin.subtitles.edit', compact('movieSubs','movieTitle'));
}
My edit.blade.php - here is where I want to fetch the data that I use old
{{ Form::select('movieTitle', $movieTitle, old('movie_id', $movieTitle) ,['class' => 'form-control'])}}<br>
Do you have an input with the name movie_id? In my understanding, the dropdown movieTitle already contains the ID of the movie so this might work better.
{{ Form::select('movieTitle', $movieTitle, old('movieTitle'), ['class' => 'form-control'])}}

Fill laravel select with eloquent model using pluck

I'm trying to fill a laravel (version 5.5) select with data from the eloquent model.
So I'm geting data like this:
$types = Type::pluck('name', 'id');
And returning to the view:
return view('something')->with('types', $types->all());
So here is my view select:
{{ Form::select('type', $types, NULL, ['id' => 'myselect', 'class' => 'form-control']) }}
Well, the problem is: each option is an object, like this:
{"id":1,"name":"test","created_at":"2017-12-29 18:09:45","updated_at":"2017-12-29 18:09:45"}
And what I want is: the value equal to the id and the name equal to the name
How can I do this?
Change this:
return view('something')->with('types', $types->all());
To:
return view('something')->with('types', $types);
try this :
$types = Type::pluck('name', 'id')->toArray();
return view('something', compact('types'));

Laravel Pluck - how to get more than one field

How can I show more than the first name? Select and pluck use key => value method (via id here), where do I write other columns I would like to bring into view ( such as last_name, paygrade, etc.)?
I would like to keep using the eloquent convention and show more info on the selected option.
Controller
$users = User::select('id', 'first_name')->pluck('first_name', 'id');
Blade
{!! Form::select('worker_id', $users, isset($users) ? $users : null, array('class' => 'form-control chosen-select', 'data-placeholder'=> 'Worker...', 'multiple' => 'multiple')) !!}
you can use this:
$users = User::selectRaw('id, CONCAT(first_name," ",last_name) as full_name')->pluck('full_name', 'id');
or this:
$users = User::select('id', DB::raw("concat(first_name, ' ', last_name) as full_name")->pluck('full_name', 'id');

Convert collection of models into an array with id as key

I am using Laravel HTML component to create a dropdown to list all the groups to which an user can belong.
The list of groups comes from a Groups table.
Currently in my controller my code looks like
$groups = array();
$groupModels = Group::all(['id', 'name']);
foreach ($groupModels as $groupModel) {
$groups[$groupModel->id] = $groupModel->name;
}
return view('myview', compact('groups'));
and in my view I have the following code to create the dropdown
{!! Form::select('group', $groups, null, ['class' => 'form-control']) !!}
This works, but I am trying to see if there is a way to avoid the foreach loop and directly convert the list of Models into an array. Is it possible?
Use pluck() method:
$groups = Group::pluck('name', 'id');
return view('myview', compact('groups'));

Updating record in Laravel from a select using laravel's lists not working

In one of my laravel pages I am updating a record. The form is bound to the model, and all fields are updating properly except those where I am presenting a select using lists that populates the select from the database:
{{ Form::select('resume_id', $resume_lists) }}
I just have no idea why these will not update. They are pulling the appropriate values from mySQL. Any ideas?
Thank you.
I have my code in routes, not in a controller
Route::get('application/edit/{id}', array('as' => 'application.edit', function($id)
{
$user = Auth::user();
$company_lists = Company::where('user_id', '=', $user->id)->get()->lists('company', 'id');
$resume_lists = Resume::where('user_id', '=', $user->id)->get()->lists('name', 'id'); //changed resume to name
$companies = Company::where('user_id', '=', Auth::user()->id)->get(); //just added
//$currentintdate=$application['followupBy']; /////
Session::put('appid', $id); /////
return View::make('application-edit', array('company_lists' => $company_lists), array('resume_lists' => $resume_lists))
->with('application', Application::find($id));
}));
try this:
$resume_lists = YourResumeModel::lists('title', 'id');
{{ Form::select('resume_id', $resume_lists) }}
frist column is your text for dropdown
and next column is your row id
just var dump the resume list data in controller, make sure its available at the controller, so after you initialize the variable/array
return var_dump($resume_lists); // check if its valid array with id as key and label as value, if available, go view and do the same
Use: $resume_lists = Resume::all()->where('user_id', '=', $user->id)->lists('name', 'id');
Or: $resume_lists = Resume::where('user_id', '=', $user->id)->lists('name', 'id')->toArray();
well, my records were not updating because I had a column as not nullable and I was not passing any value while testing. I got no error at all about this so I had no idea.

Categories