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();
Related
I'm trying use a whereIn inside a where array I am passing to Laravel query Builder:
$where = [['Participants.Client_Id','IN', $clientId]];
DB::table('Participants')->where($where)->get()
Something like is what I want to achieve, and I know there are works around such as using whereIn, but I'm sharing here a small piece of code to give you an idea, so I need to change the array to make it works as a whereIn, not changing the ->where to ->whereIn or ->whereRaw
DB::table('participants)->whereIn('Participants.Client_Id',$clientId)->get();
You must collect the IDs in the $clientId variables.
If I understand, you could do something like that :
$wheres = [['Participants.Client_Id','IN', [$clientId]]];
$query = DB::table('Participants');
foreach($wheres as $where) {
$query->where($where[0], $where[1], $where[2]);
}
$participants = $query->get();
As laravel document , you can use array in where and each element of this array must be a array with three value . So your $where variable is correct.
But as I searched in operator is not supported by query builder of where.
Sorting a unique collection by value. I've tried sortBy but instead of the key need to sort by the value
return ($c->flatten()->unique());
Output: Need to go in chronological order
{"0":"9am","1":"10am","2":"11am","3":"1pm","4":"2pm","5":"5pm","10":"3pm","14":"12pm"}
Assuming that $c is the collection you meant, this should work:
$sorted = $c->sortBy(function($value, $key) {
return strtotime($value);
});
Sources:
Laravel 5 Collections Sorting
How to sort time by meridiem (AM/PM)
I have some Newsletters:
$newsletters = $channel->Newsletter()->whereIn('id', $wantNewsletters)->get();
Which result in this collection:
I tried to use pluck, but it just allows one column:
$newsletters = $channel->Newsletter()->whereIn('id', $wantNewsletters)->pluck('media', 'id');
What I try to achieve is this (pardon my sad paintshop skillz :D)
In a way, that's like ->pluck('*', 'id'). For now I had to foreach every collection and this does not seem right to me.
Thanks for your help!
You can use keyBy method:
$newsletters = $channel->Newsletter()->whereIn('id', $wantNewsletters)->get()->keyBy('id');
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();
I have a model like this-
$feature_project = FeatureProject::select('feature_id')
->where('project_id', $project->id)
->get();
And if I return it, I am getting a output like this-
[
{
"feature_id": 2
},
{
"feature_id": 4
},
{
"feature_id": 9
}
]
But I want t output like this-
[2,4,9]
So I need to convert the output.
But I am not finding a way without using for-each loop (make a temp array, push all elements to that array from current array with a for-each loop).
But I think there is more smart way than that in Laravel to do that.
I think Laravel Collection is used for this purpose.
You can call pluck() method on the query builder.
$feature_project = FeatureProject::select('feature_id')
->where('project_id', $project->id)
->pluck('feature_id'); // [2,4,9]
https://laravel.com/api/5.2/Illuminate/Database/Eloquent/Builder.html#method_lists
Alternatively, you can use PHP's array_column() function for raw arrays.
http://php.net/manual/en/function.array-column.php
In Laravel's collections, you can call a method called Flatten, which flattens a multi-dimensional collection into a single dimension.
https://laravel.com/docs/5.2/collections#method-flatten
$collection = collect(['name' => 'taylor', 'languages' => ['php', 'javascript']]);
$flattened = $collection->flatten();
$flattened->all();
// ['taylor', 'php', 'javascript'];
With a fairly flat object, it should return just the values.
Use pluck():
$feature_project = FeatureProject::where('project_id', $project->id)->pluck('feature_id');
An alternative way also will be helpful in some cases.
We can run raw queries inside select function.
Here is an example:
$feature_project = FeatureProject::select(DB::raw('GROUP_CONCAT("feature_id")))
->where('project_id', $project->id)
->get();
In DB::raw we can run mysql query with function and case same as mysql query.
You can use lists() and toArray() :
$feature_project=FeatureProject::where('project_id', $project->id)->lists('id')->toArray();
Hope this helps.