Laravel Pluck - how to get more than one field - php

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');

Related

Form select box {!!Form::select()!!} Laravel

I'm using Laravel Collective Form builder in my view and i'm populating this select box from a table in my DB like this
I am having an issue with my values not matching up and my dropdown is also giving me the values as an array...
Here's what I have in my PostsController:-
public function edit(Post $post)
{
$categories = Category::all()->pluck('title', 'id')->toArray();
return view('posts.edit')->withPost($post)->withCategories($categories);
}
and here's my view edit.blade.php:-
{{ Form::label('category_id', 'Category :')}}
{!! Form::select('category_id', $categories, null, ['class' => 'form-control']) !!}
So I need little help?
Here's the value issue I was talking about:
enter image description here
Here's the array issue I was talking about:
enter image description here
no need to use toArray() pluck method automatically create an array.
try this
$categories = Category::pluck('title', 'id');
First of all your code seems like not proper.
$categories = Category::all()->pluck('title', 'id')->toArray();
should be
$categories = Category::pluck('title', 'id')->toArray();
Firstly update you controller function. I think that will help:-
public function edit(Post $post)
{
$categories = Category::pluck('title', 'id')->toArray();
return view('posts.edit', [
'post' => $post,
'categories' => $categories
]);
}
Try this code and let me know need help.

Laravel 5.4 pluck an entire list

In my Laravel model controller I have this:
public function guestInfo($id)
{
$guests = Person::where('id', $id)
->with('languages')
->get();
return view('layouts.visitor', ['guests' => $guests]);
}
in my blade file I have this:
#foreach ($guests as $guest)
some html stuff in here
{!! Form::select('name', $guest->languages->pluck('name')->all(), ['class' => 'form-control']) !!}
more html
#endforeach
Person is a model to a db table "persons" and "languages" is a relative model "belongs to" Person.
In my languages table I have different rows for languages: "english, spanish, etc" each is in it's own row with it's own id.
My goal is to get all the languages to show up. However currently with the above code I only get one language to show up.
Any ideas?
your code will retrieve only that languages which associated with persons. if you want to show all languages, just retrieve them as id => name pairs and pass to your view.
$guests = Person::where('id', $id)->get();
$languagess = Language::pluck('name', 'id);
and then do foreach with $languages
#foreach ($languages as $key => $val)
{!! Form::select('name', $key, ['class' => 'form-control']) !!}
#endforeach
I ended up doing it like this with some help from #devnull Ψ. Not exactly as he suggested but it gave me some direction.
Controller:
public function guestInfo($id)
{
$guests = Person::where('id', $id)
->with('languages')
->get();
$languages = Language::pluck('name')->toArray();
return view('layouts.visitor', ['guests' => $guests, 'languages' => $languages]);
}
Then in my blade:
{!! Form::select('name', $languages, 'name', ['placeholder' => '<select>','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'));

Dropdown menu with eloquent

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.

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'));

Categories