ModalName::pluck('id')->toArray();
i want id to be associative array with certain defined key.
such as 'my_key'=>id in a pluck
First of all pluck() return you already an array so no need to call toArray().
Yes you can make it associative by passing another argument on pluck method. Like this
$plucked = $collection->pluck('name', 'product_id');
sample result
['prod-100' => 'Desk', 'prod-200' => 'Chair']
please see docs here source
so in your case
ModalName::all()->pluck('id', 'name'); // name = field in your table
Related
I solver this by this code
$service_list = Service::all();
$services = [];
foreach ($service_list as $item){
$services[$item['id']] = $item['name'];
}
but how to do that using php_array functions?
its for dropdown select
Not sure why you have to use PHPs built in array methods but we have pluck on the Query Builder and Collection class.
$services = Service::pluck('name', 'id');
// $services->all(); // for the actual array contained
This will only select the name and id in the query and give you a Collection keyed by the id only containing the name field.
$services = Service::all();
$services_array = $services->pluck('name', 'id')->all();
If you already have your collection of models (code above has queried for every field and hydrated models with the result) you can use pluck on the Collection to achieve the same result (though less efficient as it had to query for all fields, hydrate models, then pull those 2 fields from them)
Laravel 5.5 Docs - Query Builder - Retrieving Results
Laravel 5.5 Docs - Collections - pluck method
Use toArray() to convert the collection to an array, then use array_combine() to create an associative array from that.
$service_list = Service::all()->toArray();
$services = array_combine(array_column($service_list, 'id'), array_column($service_list, 'name'));
$service_list = Service::all()->toArray();
all() will return a collection. The collection supports a toArray() method
How can convert the result from eloquent to associative array. I need to select tow column and have one as key and another as value. Here is the closet I got, however the value is an array. I want it to be only "my_value" column.
$array = Post::select('my_key','my_value')->get()->keyBy('my_key')
You should use lists (Laravel 5.1) or pluck (Laravel 5.2+):
$array = Post::lists('my_value', 'my_key');
or
$array = Post::pluck('my_value', 'my_key');
I found a way to do so, I'm not sure whether it's proper way to do this performance wise though...
$array = Post::select('my_key','my_value')->get()->mapWithKeys(function ($item) {
return [$item['my_key'] => $item['my_value']];
})->toArray();
can i retrieve single row from database like this in Laravel
["name"=>"jhon",
"age"=>"18"
]
Use the first() method:
User::where('name', 'John Smith')->first();
Or the find() method:
User::find($id);
It will return an object. If you need an array, use the toArray() method to convert an object to an array:
User::find($id)->toArray();
You can also use Eloquent Collection methods to transform it however you want.
$people = User::where('name', 'John Smith')->get()->map(function ($person) {
return [
'id' => $person->id,
'name' => $person->name
];
});
Use first() method along with id because id is unique key for every row
User::whereId($id)->first();
I am new to Laravel. I have a List that is being returned as an array in Blade
called personType, but the keys are all out of order.
I see the List being generated in the controller like this:
$personTypeList = PersonType::lists('per_type', 'id');
But this returns an array that is unsorted. I've tried ksort( $personTypeList) but it causes an error.
Anybody can shine a light on what I may be doing wrong?
how about adding an orderBy?
$personTypeList = PersonType::orderBy('per_type', 'desc')->lists('per_type', 'id');
You can use the method sort available when you are dealing with collections in laravel.
//will sort the collection by values, in ASC order.
$personTypeList = PersonType::lists('per_type', 'id') -> sort();
Hope this works for you.
You need to cast the collection to an Array.
$personTypeList = ['' => ''] + PersonType::lists('per_type', 'id')->toArray();
How to Do following Query in Laravel Eloquent?
SELECT catID, catName, imgPath FROM categories WHERE catType = "Root"
I have tried following
CategoryModel::where('catType', '=', 'Root')
->lists('catName', 'catID', 'imgPath');
but its return only two fields.
Array ( [7] => Category 1 )
lists() turns the resulting collection into an array with key value. You can only have two database columns in there. Otherwise you have to use select() but then you will get a collection of models not just an array.
$categories = CategoryModel::select('catID', 'catName', 'imgPath')
->where('catType', '=', 'Root')
->get();
Selecting multiple columns
CategoryModel::get(['catName', 'catID', 'imgPath']);
Works with Laravel 5.3 too!
If you want to get certain columns then You can use one of the two method get() or all()
but the syntax is different for both, get() method takes array as argument and all() method takes string or array both as argument:
Model::all('field1','field2') with string as arguments
CategoryModel::all('catName', 'catID', 'imgPath')->where('catType','Root');
Model::all(['field1','field2']) with array as arguments
CategoryModel::all(['catName', 'catID', 'imgPath'])->where('catType','Root');
Model::get(['field1','field2'])
CategoryModel::get(['catName', 'catID', 'imgPath'])->where('catType','Root');
CategoryModel::wherecatType('Root')
->pluck('catName', 'catID', 'imgPath');
From laravel version 5.3^ lists() is deprecated and function pluck() is used instead.
pluck() returns a collection and if you need a simple array just prepend ->toArray() to it.