Fill laravel select with eloquent model using pluck - php

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

Related

How to optimize multiple value query from database in laravel

I am new to laravel and using simple command to query from database.
I am trying to get multiple value from database with same id.
$id = 'P01';
//Get value from database
$fruitname = DB::table('fruits')->where('id', $id)->value('fruitname');
$fruitcolour = DB::table('fruits')->where('id', $id)->value('fruitcolour');
$fruitshape = DB::table('fruits')->where('id', $id)->value('fruitshape');
$updatedat = DB::table('fruits')->where('id', $id)->value('updated_at');
$data = array(
'fruitname' => $fruitname,
'fruitcolour' => $fruitcolour,
'fruitshape' => $fruitshape,
'updated_at' => $updatedat
);
As result, $data can store the value.
But i found out that it takes time to complete the process.
Is there any ways i can optimize the process?
I'm not sure why you're making it more hard than it is, but you can just simply use eloquent in this case:
Provided you already have created the necessary model and adding it in your controller:
use App\Fruits;
Just simply use it in the find method:
public function someMethodName()
{
$id = 'P01';
$fruit = Fruits::find($id);
echo $fruit->fruitname; // and others
}
$fruit = DB::table('fruits')->where('id', $id)->first();
$data = array(
'fruitname' => $fruit->fruitname,
'fruitcolour' => $fruit->fruitcolour,
'fruitshape' => $fruit->fruitshape,
'updated_at' => $fruit->updatedat
);
Or if you have a view, you could just do
$fruit = DB::table('fruits')->where('id', $id)->first();
return view('yourview', compact('fruit'));
And in the view, you can access each of the values like so:
{{ $fruit->fruitname }}
{{ $fruit->fruitcolor }}
In your controller add function
public function function_name(Fruit $fruit)
{
return view('your_view_name', compact('fruit'));
}
In web file set your route as
Route::get('/fruit/{fruit}','YourControllername#methodName')->name('show');
At the time of setting route
Show
in View file get data as
{{ $fruit->fruitname }}
{{ $fruit->fruitcolor }}
To make it with a single query with QueryBuilder since QueryBuilder can have better performance than Eloquent:
DB::table('fruits')->where('id', $id)
->select(['fruitname', 'fruitcolour', 'fruitshape', 'updated_at'])
->first();
This returns a associative array with the selected column names.

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

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