Here is the print_r of my object;
Array
(
[country] => Illuminate\Database\Eloquent\Collection Object
(
[items:protected] => Array
(
[0] => App\Models\Location Object
(
[connection:protected] =>
[table:protected] =>
[primaryKey:protected] => id
[perPage:protected] => 15
[incrementing] => 1
[timestamps] => 1
[attributes:protected] => Array
(
[country] => Scotland
)
[original:protected] => Array
(
[country] => Scotland
)
[relations:protected] => Array
(
)
[hidden:protected] => Array
(
)
[visible:protected] => Array
(
)
[appends:protected] => Array
(
)
[fillable:protected] => Array
(
)
[guarded:protected] => Array
(
[0] => *
)
[dates:protected] => Array
(
)
[casts:protected] => Array
(
)
[touches:protected] => Array
(
)
[observables:protected] => Array
(
)
[with:protected] => Array
(
)
[morphClass:protected] =>
[exists] => 1
)
[1] => App\Models\Location Object
(
[connection:protected] =>
[table:protected] =>
[primaryKey:protected] => id
[perPage:protected] => 15
[incrementing] => 1
[timestamps] => 1
[attributes:protected] => Array
(
[country] => England
)
[original:protected] => Array
(
[country] => England
)
[relations:protected] => Array
(
)
[hidden:protected] => Array
(
)
[visible:protected] => Array
(
)
[appends:protected] => Array
(
)
[fillable:protected] => Array
(
)
[guarded:protected] => Array
(
[0] => *
)
[dates:protected] => Array
(
)
[casts:protected] => Array
(
)
[touches:protected] => Array
(
)
[observables:protected] => Array
(
)
[with:protected] => Array
(
)
[morphClass:protected] =>
[exists] => 1
)
[2] => App\Models\Location Object
(
[connection:protected] =>
[table:protected] =>
[primaryKey:protected] => id
[perPage:protected] => 15
[incrementing] => 1
[timestamps] => 1
[attributes:protected] => Array
(
[country] => Wales
)
[original:protected] => Array
(
[country] => Wales
)
[relations:protected] => Array
(
)
[hidden:protected] => Array
(
)
[visible:protected] => Array
(
)
[appends:protected] => Array
(
)
[fillable:protected] => Array
(
)
[guarded:protected] => Array
(
[0] => *
)
[dates:protected] => Array
(
)
[casts:protected] => Array
(
)
[touches:protected] => Array
(
)
[observables:protected] => Array
(
)
[with:protected] => Array
(
)
[morphClass:protected] =>
[exists] => 1
)
[3] => App\Models\Location Object
(
[connection:protected] =>
[table:protected] =>
[primaryKey:protected] => id
[perPage:protected] => 15
[incrementing] => 1
[timestamps] => 1
[attributes:protected] => Array
(
[country] =>
)
[original:protected] => Array
(
[country] =>
)
[relations:protected] => Array
(
)
[hidden:protected] => Array
(
)
[visible:protected] => Array
(
)
[appends:protected] => Array
(
)
[fillable:protected] => Array
(
)
[guarded:protected] => Array
(
[0] => *
)
[dates:protected] => Array
(
)
[casts:protected] => Array
(
)
[touches:protected] => Array
(
)
[observables:protected] => Array
(
)
[with:protected] => Array
(
)
[morphClass:protected] =>
[exists] => 1
)
)
)
)
I want a foreach loop to print out the three countries in the country array - (England, Wales, Scotland).
I have tried loops such as;
#foreach ($locations['country'] as $country)
{{ $country }}
#endforeach
I have tried other variations of this but to no avail. What is the correct syntax? Also, can someone explain how I can interpret this so I can better understand foreach with arrays in future? I normally just end up guessing until I get the right result - but for a change I'd like to actually know how to put one together if that makes sense..
I am using Laravel if it helps...
What you're looping over isn't an array. It's a Laravel Collection. However it behaves like an array so it doesn't really matter. The loop itself actually looks correct. But instead of just outputting $country you have to actually access the attribute on $country called country:
#foreach($locations['country'] as $location)
{{ $location->country }}
#endforeach
In general a foreach loop goes over every item in an array or collection and put's that item into the variable you define after as. Maybe this explanation helps too.
As a little extra: Laravel has a nice lists() function which builds an array out of an attribute from every model in a collection.
$countries = $locations['country']->lists('country');
Would result in something like:
['England', 'Wales', 'Scotland']
And you could then use functions like implode() to generate a comma separated list:
implode(', ', $countries); // returns 'England, Wales, Scotland'
Related
I have a problem with Laravel 8 and I'm not sure how to identify the error.
The following happens ..
In my controller it looks like this:
$var = model::where('name', $name)->firstOrFail();
print_r($var)
Print_r returns this to me here
App\Models\test Object (
[table:protected] => tb_test
[timestamps] =>
[connection:protected] => mysql
[primaryKey:protected] => id
[keyType:protected] => int
[incrementing] => 1
[with:protected] => Array ( )
[withCount:protected] => Array ( )
[perPage:protected] => 15
[exists] => 1
[wasRecentlyCreated] =>
[attributes:protected] => Array (
[id] => 48494980
[name] => TEST
)
[original:protected] => Array (
[id] => 48494980
[name] => TEST
)
[changes:protected] => Array ( )
[casts:protected] => Array ( )
[classCastCache:protected] => Array ( )
[dates:protected] => Array ( )
[dateFormat:protected] =>
[appends:protected] => Array ( )
[dispatchesEvents:protected] => Array ( )
[observables:protected] => Array ( )
[relations:protected] => Array ( )
[touches:protected] => Array ( )
[hidden:protected] => Array ( )
[visible:protected] => Array ( )
[fillable:protected] => Array ( )
[guarded:protected] => Array ( [0] => * )
)
when I try to use foreach, it returns me Attempt to read property “name” on bool
Can someone please tell me what I'm doing wrong?
If you want to walk the attributes one at a time, use toArray()
$var = model::where('name', $name)->firstOrFail();
$arrayVar = $var->toArray();
foreach( $arrayVar as $name => value) {
//....
}
I have a query that returns a collection object like so:
Illuminate\Database\Eloquent\Collection Object
(
[items:protected] => Array
(
[0] => App\Http\Models\LabTestRequests Object
(
[table:protected] => lab_test_requests
[primaryKey:protected] => request_id
[connection:protected] =>
[keyType:protected] => int
[perPage:protected] => 15
[incrementing] => 1
[timestamps] => 1
[attributes:protected] => Array
(
[status_category] => Old Results
)
[original:protected] => Array
(
[status_category] => Old Results
)
[relations:protected] => Array
(
)
[hidden:protected] => Array
(
)
[visible:protected] => Array
(
)
[appends:protected] => Array
(
)
[fillable:protected] => Array
(
)
[guarded:protected] => Array
(
[0] => *
)
[dates:protected] => Array
(
)
[dateFormat:protected] =>
[casts:protected] => Array
(
)
[touches:protected] => Array
(
)
[observables:protected] => Array
(
)
[with:protected] => Array
(
)
[exists] => 1
[wasRecentlyCreated] =>
)
[1] => App\Http\Models\LabTestRequests Object
(
[table:protected] => lab_test_requests
[primaryKey:protected] => request_id
[connection:protected] =>
[keyType:protected] => int
[perPage:protected] => 15
[incrementing] => 1
[timestamps] => 1
[attributes:protected] => Array
(
[status_category] => Pending
)
[original:protected] => Array
(
[status_category] => Pending
)
[relations:protected] => Array
(
)
[hidden:protected] => Array
(
)
[visible:protected] => Array
(
)
[appends:protected] => Array
(
)
[fillable:protected] => Array
(
)
[guarded:protected] => Array
(
[0] => *
)
[dates:protected] => Array
(
)
[dateFormat:protected] =>
[casts:protected] => Array
(
)
[touches:protected] => Array
(
)
[observables:protected] => Array
(
)
[with:protected] => Array
(
)
[exists] => 1
[wasRecentlyCreated] =>
)
[2] => App\Http\Models\LabTestRequests Object
(
[table:protected] => lab_test_requests
[primaryKey:protected] => request_id
[connection:protected] =>
[keyType:protected] => int
[perPage:protected] => 15
[incrementing] => 1
[timestamps] => 1
[attributes:protected] => Array
(
[status_category] => Done
)
[original:protected] => Array
(
[status_category] => Done
)
[relations:protected] => Array
(
)
[hidden:protected] => Array
(
)
[visible:protected] => Array
(
)
[appends:protected] => Array
(
)
[fillable:protected] => Array
(
)
[guarded:protected] => Array
(
[0] => *
)
[dates:protected] => Array
(
)
[dateFormat:protected] =>
[casts:protected] => Array
(
)
[touches:protected] => Array
(
)
[observables:protected] => Array
(
)
[with:protected] => Array
(
)
[exists] => 1
[wasRecentlyCreated] =>
)
[3] => App\Http\Models\LabTestRequests Object
(
[table:protected] => lab_test_requests
[primaryKey:protected] => request_id
[connection:protected] =>
[keyType:protected] => int
[perPage:protected] => 15
[incrementing] => 1
[timestamps] => 1
[attributes:protected] => Array
(
[status_category] => Non-Negative
)
[original:protected] => Array
(
[status_category] => Non-Negative
)
[relations:protected] => Array
(
)
[hidden:protected] => Array
(
)
[visible:protected] => Array
(
)
[appends:protected] => Array
(
)
[fillable:protected] => Array
(
)
[guarded:protected] => Array
(
[0] => *
)
[dates:protected] => Array
(
)
[dateFormat:protected] =>
[casts:protected] => Array
(
)
[touches:protected] => Array
(
)
[observables:protected] => Array
(
)
[with:protected] => Array
(
)
[exists] => 1
[wasRecentlyCreated] =>
)
[4] => App\Http\Models\LabTestRequests Object
(
[table:protected] => lab_test_requests
[primaryKey:protected] => request_id
[connection:protected] =>
[keyType:protected] => int
[perPage:protected] => 15
[incrementing] => 1
[timestamps] => 1
[attributes:protected] => Array
(
[status_category] => Test Complete Pending Review
)
[original:protected] => Array
(
[status_category] => Test Complete Pending Review
)
[relations:protected] => Array
(
)
[hidden:protected] => Array
(
)
[visible:protected] => Array
(
)
[appends:protected] => Array
(
)
[fillable:protected] => Array
(
)
[guarded:protected] => Array
(
[0] => *
)
[dates:protected] => Array
(
)
[dateFormat:protected] =>
[casts:protected] => Array
(
)
[touches:protected] => Array
(
)
[observables:protected] => Array
(
)
[with:protected] => Array
(
)
[exists] => 1
[wasRecentlyCreated] =>
)
)
)
In my view im iterating through each and showing the status_category value - what im trying to accomplish is to check if a specific status_category value does not exist then to add it to the end.
I have tried doing:
$categories->prepend('test', 'status_category');
but that doesnt add it like the object above and therefore my view will throw an error. Any ideas on this?
You need to use the laravel collection map function for set value in object key value.
$categories = $categories->map(function ($item, $key) {
$item->status_category = "test";
return $item;
});
You can achieve via
1) Laravel Collection map()
$labTestRequests = $labTestRequests->map(function($labTestRequest){
if(!isset($labTestRequest->status_category)) { // checking if a key exist
$labTestRequest->status_category = "default";
}
return $labTestRequest;
});
2) Using a simple null coalesce operator i.e. ??. What it does is if a given variable does not exists or is null then use the given default instead. In your view you can write like this
$labTestRequest->status_category ?? 'default'
I'm trying to display a list of names from a query in Laravel, I can't manage to make the results show properly.
Here's what I've got in my controller:
public function getSettings()
{
$contents = explode(",", File::get(storage_path('app/public/users/0/config/social_module/buddy.txt')));
foreach($contents as $key => $value){
$buddylist[$value] = User::select('fullname as name')->where('id', $value)->get();
}
return view('welcome', compact('buddylist'));
}
Here's my view looks like:
#foreach($buddylist as $buddy)
<ul>
<li>{{ $buddy }}</li>
</ul>
#endforeach
If I use $buddy=>$key it shows nothing.
And if I use this instead, this is what the results look like:
[{"name":"Alix Earnshaw"}]
[{"name":"Horacio Esgate"}]
[{"name":"Patrizia Anthony"}]
[{"name":"Ann-marie McLewd"}]
[{"name":"Boote Poulsum"}]
[{"name":"Any Bandy"}]
[{"name":"Francene Lelliott"}]
[{"name":"Ingelbert Abdy"}]
[{"name":"Nickolaus Di Biaggi"}]
[{"name":"Constantina Wreight"}]
[{"name":"Clarissa Coomer"}]
print_r($contents)
Array ( [0] => 1 [1] => 2 [2] => 3 [3] => 4 [4] => 5 [5] => 12 [6] => 43 [7] => 74 [8] => 23 [9] => 67 [10] => 32 )
print_r($buddylist)
Array ( [1] => Illuminate\Database\Eloquent\Collection Object ( [items:protected] => Array ( [0] => App\User Object ( [table:protected] => users [fillable:protected] => Array ( ) [hidden:protected] => Array ( [0] => password ) [primaryKey:protected] => id [keyType:protected] => bigInteger [timestamps] => [remember_token] => [connection:protected] => mysql [incrementing] => 1 [with:protected] => Array ( ) [withCount:protected] => Array ( ) [perPage:protected] => 15 [exists] => 1 [wasRecentlyCreated] => [attributes:protected] => Array ( [name] => Alix Earnshaw ) [original:protected] => Array ( [name] => Alix Earnshaw ) [changes:protected] => Array ( ) [casts:protected] => Array ( ) [dates:protected] => Array ( ) [dateFormat:protected] => [appends:protected] => Array ( ) [dispatchesEvents:protected] => Array ( ) [observables:protected] => Array ( ) [relations:protected] => Array ( ) [touches:protected] => Array ( ) [visible:protected] => Array ( ) [guarded:protected] => Array ( [0] => * ) ) ) ) [2] => Illuminate\Database\Eloquent\Collection Object ( [items:protected] => Array ( [0] => App\User Object ( [table:protected] => users [fillable:protected] => Array ( ) [hidden:protected] => Array ( [0] => password ) [primaryKey:protected] => id [keyType:protected] => bigInteger [timestamps] => [remember_token] => [connection:protected] => mysql [incrementing] => 1 [with:protected] => Array ( ) [withCount:protected] => Array ( ) [perPage:protected] => 15 [exists] => 1 [wasRecentlyCreated] => [attributes:protected] => Array ( [name] => Horacio Esgate ) [original:protected] => Array ( [name] => Horacio Esgate ) [changes:protected] => Array ( ) [casts:protected] => Array ( ) [dates:protected] => Array ( ) [dateFormat:protected] => [appends:protected] => Array ( ) [dispatchesEvents:protected] => Array ( ) [observables:protected] => Array ( ) [relations:protected] => Array ( ) [touches:protected] => Array ( ) [visible:protected] => Array ( ) [guarded:protected] => Array ( [0] => * ) ) ) ) [3] => Illuminate\Database\Eloquent\Collection Object ( [items:protected] => Array ( [0] => App\User Object ( [table:protected] => users [fillable:protected] => Array ( ) [hidden:protected] => Array ( [0] => password ) [primaryKey:protected] => id [keyType:protected] => bigInteger [timestamps] => [remember_token] => [connection:protected] => mysql [incrementing] => 1 [with:protected] => Array ( ) [withCount:protected] => Array ( ) [perPage:protected] => 15 [exists] => 1 [wasRecentlyCreated] => [attributes:protected] => Array ( [name] => Patrizia Anthony ) [original:protected] => Array ( [name] => Patrizia Anthony ) [changes:protected] => Array ( ) [casts:protected] => Array ( ) [dates:protected] => Array ( ) [dateFormat:protected] => [appends:protected] => Array ( ) [dispatchesEvents:protected] => Array ( ) [observables:protected] => Array ( ) [relations:protected] => Array ( ) [touches:protected] => Array ( ) [visible:protected] => Array ( ) [guarded:protected] => Array ( [0] => * ) ) ) ) [4] => Illuminate\Database\Eloquent\Collection Object ( [items:protected] => Array ( [0] => App\User Object ( [table:protected] => users [fillable:protected] => Array ( ) [hidden:protected] => Array ( [0] => password ) [primaryKey:protected] => id [keyType:protected] => bigInteger [timestamps] => [remember_token] => [connection:protected] => mysql [incrementing] => 1 [with:protected] => Array ( ) [withCount:protected] => Array ( ) [perPage:protected] => 15 [exists] => 1 [wasRecentlyCreated] => [attributes:protected] => Array ( [name] => Ann-marie McLewd ) [original:protected] => Array ( [name] => Ann-marie McLewd ) [changes:protected] => Array ( ) [casts:protected] => Array ( ) [dates:protected] => Array ( ) [dateFormat:protected] => [appends:protected] => Array ( ) [dispatchesEvents:protected] => Array ( ) [observables:protected] => Array ( ) [relations:protected] => Array ( ) [touches:protected] => Array ( ) [visible:protected] => Array ( ) [guarded:protected] => Array ( [0] => * ) ) ) ) [5] => Illuminate\Database\Eloquent\Collection Object ( [items:protected] => Array ( [0] => App\User Object ( [table:protected] => users [fillable:protected] => Array ( ) [hidden:protected] => Array ( [0] => password ) [primaryKey:protected] => id [keyType:protected] => bigInteger [timestamps] => [remember_token] => [connection:protected] => mysql [incrementing] => 1 [with:protected] => Array ( ) [withCount:protected] => Array ( ) [perPage:protected] => 15 [exists] => 1 [wasRecentlyCreated] => [attributes:protected] => Array ( [name] => Boote Poulsum ) [original:protected] => Array ( [name] => Boote Poulsum ) [changes:protected] => Array ( ) [casts:protected] => Array ( ) [dates:protected] => Array ( ) [dateFormat:protected] => [appends:protected] => Array ( ) [dispatchesEvents:protected] => Array ( ) [observables:protected] => Array ( ) [relations:protected] => Array ( ) [touches:protected] => Array ( ) [visible:protected] => Array ( ) [guarded:protected] => Array ( [0] => * ) ) ) ) [12] => Illuminate\Database\Eloquent\Collection Object ( [items:protected] => Array ( [0] => App\User Object ( [table:protected] => users [fillable:protected] => Array ( ) [hidden:protected] => Array ( [0] => password ) [primaryKey:protected] => id [keyType:protected] => bigInteger [timestamps] => [remember_token] => [connection:protected] => mysql [incrementing] => 1 [with:protected] => Array ( ) [withCount:protected] => Array ( ) [perPage:protected] => 15 [exists] => 1 [wasRecentlyCreated] => [attributes:protected] => Array ( [name] => Any Bandy ) [original:protected] => Array ( [name] => Any Bandy ) [changes:protected] => Array ( ) [casts:protected] => Array ( ) [dates:protected] => Array ( ) [dateFormat:protected] => [appends:protected] => Array ( ) [dispatchesEvents:protected] => Array ( ) [observables:protected] => Array ( ) [relations:protected] => Array ( ) [touches:protected] => Array ( ) [visible:protected] => Array ( ) [guarded:protected] => Array ( [0] => * ) ) ) ) [43] => Illuminate\Database\Eloquent\Collection Object ( [items:protected] => Array ( [0] => App\User Object ( [table:protected] => users [fillable:protected] => Array ( ) [hidden:protected] => Array ( [0] => password ) [primaryKey:protected] => id [keyType:protected] => bigInteger [timestamps] => [remember_token] => [connection:protected] => mysql [incrementing] => 1 [with:protected] => Array ( ) [withCount:protected] => Array ( ) [perPage:protected] => 15 [exists] => 1 [wasRecentlyCreated] => [attributes:protected] => Array ( [name] => Francene Lelliott ) [original:protected] => Array ( [name] => Francene Lelliott ) [changes:protected] => Array ( ) [casts:protected] => Array ( ) [dates:protected] => Array ( ) [dateFormat:protected] => [appends:protected] => Array ( ) [dispatchesEvents:protected] => Array ( ) [observables:protected] => Array ( ) [relations:protected] => Array ( ) [touches:protected] => Array ( ) [visible:protected] => Array ( ) [guarded:protected] => Array ( [0] => * ) ) ) ) [74] => Illuminate\Database\Eloquent\Collection Object ( [items:protected] => Array ( [0] => App\User Object ( [table:protected] => users [fillable:protected] => Array ( ) [hidden:protected] => Array ( [0] => password ) [primaryKey:protected] => id [keyType:protected] => bigInteger [timestamps] => [remember_token] => [connection:protected] => mysql [incrementing] => 1 [with:protected] => Array ( ) [withCount:protected] => Array ( ) [perPage:protected] => 15 [exists] => 1 [wasRecentlyCreated] => [attributes:protected] => Array ( [name] => Ingelbert Abdy ) [original:protected] => Array ( [name] => Ingelbert Abdy ) [changes:protected] => Array ( ) [casts:protected] => Array ( ) [dates:protected] => Array ( ) [dateFormat:protected] => [appends:protected] => Array ( ) [dispatchesEvents:protected] => Array ( ) [observables:protected] => Array ( ) [relations:protected] => Array ( ) [touches:protected] => Array ( ) [visible:protected] => Array ( ) [guarded:protected] => Array ( [0] => * ) ) ) ) [23] => Illuminate\Database\Eloquent\Collection Object ( [items:protected] => Array ( [0] => App\User Object ( [table:protected] => users [fillable:protected] => Array ( ) [hidden:protected] => Array ( [0] => password ) [primaryKey:protected] => id [keyType:protected] => bigInteger [timestamps] => [remember_token] => [connection:protected] => mysql [incrementing] => 1 [with:protected] => Array ( ) [withCount:protected] => Array ( ) [perPage:protected] => 15 [exists] => 1 [wasRecentlyCreated] => [attributes:protected] => Array ( [name] => Nickolaus Di Biaggi ) [original:protected] => Array ( [name] => Nickolaus Di Biaggi ) [changes:protected] => Array ( ) [casts:protected] => Array ( ) [dates:protected] => Array ( ) [dateFormat:protected] => [appends:protected] => Array ( ) [dispatchesEvents:protected] => Array ( ) [observables:protected] => Array ( ) [relations:protected] => Array ( ) [touches:protected] => Array ( ) [visible:protected] => Array ( ) [guarded:protected] => Array ( [0] => * ) ) ) ) [67] => Illuminate\Database\Eloquent\Collection Object ( [items:protected] => Array ( [0] => App\User Object ( [table:protected] => users [fillable:protected] => Array ( ) [hidden:protected] => Array ( [0] => password ) [primaryKey:protected] => id [keyType:protected] => bigInteger [timestamps] => [remember_token] => [connection:protected] => mysql [incrementing] => 1 [with:protected] => Array ( ) [withCount:protected] => Array ( ) [perPage:protected] => 15 [exists] => 1 [wasRecentlyCreated] => [attributes:protected] => Array ( [name] => Constantina Wreight ) [original:protected] => Array ( [name] => Constantina Wreight ) [changes:protected] => Array ( ) [casts:protected] => Array ( ) [dates:protected] => Array ( ) [dateFormat:protected] => [appends:protected] => Array ( ) [dispatchesEvents:protected] => Array ( ) [observables:protected] => Array ( ) [relations:protected] => Array ( ) [touches:protected] => Array ( ) [visible:protected] => Array ( ) [guarded:protected] => Array ( [0] => * ) ) ) ) [32] => Illuminate\Database\Eloquent\Collection Object ( [items:protected] => Array ( [0] => App\User Object ( [table:protected] => users [fillable:protected] => Array ( ) [hidden:protected] => Array ( [0] => password ) [primaryKey:protected] => id [keyType:protected] => bigInteger [timestamps] => [remember_token] => [connection:protected] => mysql [incrementing] => 1 [with:protected] => Array ( ) [withCount:protected] => Array ( ) [perPage:protected] => 15 [exists] => 1 [wasRecentlyCreated] => [attributes:protected] => Array ( [name] => Clarissa Coomer ) [original:protected] => Array ( [name] => Clarissa Coomer ) [changes:protected] => Array ( ) [casts:protected] => Array ( ) [dates:protected] => Array ( ) [dateFormat:protected] => [appends:protected] => Array ( ) [dispatchesEvents:protected] => Array ( ) [observables:protected] => Array ( ) [relations:protected] => Array ( ) [touches:protected] => Array ( ) [visible:protected] => Array ( ) [guarded:protected] => Array ( [0] => * ) ) ) ) )
use first() instead of get() -
$buddylist[$value] = User::select('fullname as name')->where('id', $value)->first();
And in your view -
#foreach($buddylist as $buddy)
<ul>
<li>{{ $buddy->name }}</li>
</ul>
#endforeach
I need to build a custom sort on an object array but i'm not sure where to start. I've seen the functions usort() etc but I can't see how i'd implement these functions for what I require.
My application is returning the below object array. I need to order it by the "slot" field in the following order: All Day, Morning, Afternoon.
Illuminate\Database\Eloquent\Collection Object
(
[items:protected] => Array
(
[0] => App\BookingDates Object
(
[connection:protected] =>
[table:protected] =>
[primaryKey:protected] => id
[perPage:protected] => 15
[incrementing] => 1
[timestamps] => 1
[attributes:protected] => Array
(
[id] => 265
[slot] => Morning
)
[original:protected] => Array
(
[id] => 265
[slot] => Morning
)
[relations:protected] => Array
(
)
[hidden:protected] => Array
(
)
[visible:protected] => Array
(
)
[appends:protected] => Array
(
)
[fillable:protected] => Array
(
)
[guarded:protected] => Array
(
[0] => *
)
[dates:protected] => Array
(
)
[casts:protected] => Array
(
)
[touches:protected] => Array
(
)
[observables:protected] => Array
(
)
[with:protected] => Array
(
)
[morphClass:protected] =>
[exists] => 1
)
[1] => App\BookingDates Object
(
[connection:protected] =>
[table:protected] =>
[primaryKey:protected] => id
[perPage:protected] => 15
[incrementing] => 1
[timestamps] => 1
[attributes:protected] => Array
(
[id] => 266
[slot] => Afternoon
)
[original:protected] => Array
(
[id] => 266
[slot] => Afternoon
)
[relations:protected] => Array
(
)
[hidden:protected] => Array
(
)
[visible:protected] => Array
(
)
[appends:protected] => Array
(
)
[fillable:protected] => Array
(
)
[guarded:protected] => Array
(
[0] => *
)
[dates:protected] => Array
(
)
[casts:protected] => Array
(
)
[touches:protected] => Array
(
)
[observables:protected] => Array
(
)
[with:protected] => Array
(
)
[morphClass:protected] =>
[exists] => 1
)
[2] => App\BookingDates Object
(
[connection:protected] =>
[table:protected] =>
[primaryKey:protected] => id
[perPage:protected] => 15
[incrementing] => 1
[timestamps] => 1
[attributes:protected] => Array
(
[id] => 267
[slot] => All Day
)
[original:protected] => Array
(
[id] => 267
[slot] => All Day
)
[relations:protected] => Array
(
)
[hidden:protected] => Array
(
)
[visible:protected] => Array
(
)
[appends:protected] => Array
(
)
[fillable:protected] => Array
(
)
[guarded:protected] => Array
(
[0] => *
)
[dates:protected] => Array
(
)
[casts:protected] => Array
(
)
[touches:protected] => Array
(
)
[observables:protected] => Array
(
)
[with:protected] => Array
(
)
[morphClass:protected] =>
[exists] => 1
)
)
)
The result i'm looking for is this:
Illuminate\Database\Eloquent\Collection Object
(
[items:protected] => Array
(
[0] => App\BookingDates Object
(
[connection:protected] =>
[table:protected] =>
[primaryKey:protected] => id
[perPage:protected] => 15
[incrementing] => 1
[timestamps] => 1
[attributes:protected] => Array
(
[id] => 267
[slot] => All Day
)
[original:protected] => Array
(
[id] => 267
[slot] => All Day
)
[relations:protected] => Array
(
)
[hidden:protected] => Array
(
)
[visible:protected] => Array
(
)
[appends:protected] => Array
(
)
[fillable:protected] => Array
(
)
[guarded:protected] => Array
(
[0] => *
)
[dates:protected] => Array
(
)
[casts:protected] => Array
(
)
[touches:protected] => Array
(
)
[observables:protected] => Array
(
)
[with:protected] => Array
(
)
[morphClass:protected] =>
[exists] => 1
)
[1] => App\BookingDates Object
(
[connection:protected] =>
[table:protected] =>
[primaryKey:protected] => id
[perPage:protected] => 15
[incrementing] => 1
[timestamps] => 1
[attributes:protected] => Array
(
[id] => 265
[slot] => Morning
)
[original:protected] => Array
(
[id] => 265
[slot] => Morning
)
[relations:protected] => Array
(
)
[hidden:protected] => Array
(
)
[visible:protected] => Array
(
)
[appends:protected] => Array
(
)
[fillable:protected] => Array
(
)
[guarded:protected] => Array
(
[0] => *
)
[dates:protected] => Array
(
)
[casts:protected] => Array
(
)
[touches:protected] => Array
(
)
[observables:protected] => Array
(
)
[with:protected] => Array
(
)
[morphClass:protected] =>
[exists] => 1
)
[2] => App\BookingDates Object
(
[connection:protected] =>
[table:protected] =>
[primaryKey:protected] => id
[perPage:protected] => 15
[incrementing] => 1
[timestamps] => 1
[attributes:protected] => Array
(
[id] => 266
[slot] => Afternoon
)
[original:protected] => Array
(
[id] => 266
[slot] => Afternoon
)
[relations:protected] => Array
(
)
[hidden:protected] => Array
(
)
[visible:protected] => Array
(
)
[appends:protected] => Array
(
)
[fillable:protected] => Array
(
)
[guarded:protected] => Array
(
[0] => *
)
[dates:protected] => Array
(
)
[casts:protected] => Array
(
)
[touches:protected] => Array
(
)
[observables:protected] => Array
(
)
[with:protected] => Array
(
)
[morphClass:protected] =>
[exists] => 1
)
)
)
You can try something like this
public function index()
{
$bookingsDate = BookingDate::all() // Your actual collection
$this->sortCollection($bookingDate);
}
public function sortCollection(\Collection &$bookingDate)
{
$bookingDate->sortBy(function($date)
{
$slots = [
'Afternoon' => 1,
'Morning' => 2,
'All day' => 3,
];
$slot = $slots[$date['slot']];
return $slot;
});
}
The sortBy method will do an ascending sort, for descending sort, try sortByDesc
See : http://laravel.com/docs/5.1/collections#method-sortby
I am working on a project using Laravel 4.2 and I have a line that retrieves some rows from a table called Gifts
$theGifts = Gift::whereIn('id', $giftImages)->get();
This variable $giftImages is an array and values are ordered like this
Array
(
[0] => 5
[1] => 2
[2] => 3
[3] => 4
)
When I output this one however $theGifts the retrieved results are not in the same order as the array of ids is and I need it to be in the same order..
How can I insure that the order is the same? I am using the data from the object in the controller, not the view by the way.
The printout of the Gift::all() looks like this (in case someone needs to compare)..
Illuminate\Database\Eloquent\Collection Object
(
[items:protected] => Array
(
[0] => Gift Object
(
[table:protected] => gifts
[connection:protected] =>
[primaryKey:protected] => id
[perPage:protected] => 15
[incrementing] => 1
[timestamps] => 1
[attributes:protected] => Array
(
[id] => 2
[name] => Banana
[image] => bananas-icon.png
[value] => 10
)
[original:protected] => Array
(
[id] => 2
[name] => Banana
[image] => bananas-icon.png
[value] => 10
)
[relations:protected] => Array
(
)
[hidden:protected] => Array
(
)
[visible:protected] => Array
(
)
[appends:protected] => Array
(
)
[fillable:protected] => Array
(
)
[guarded:protected] => Array
(
[0] => *
)
[dates:protected] => Array
(
)
[touches:protected] => Array
(
)
[observables:protected] => Array
(
)
[with:protected] => Array
(
)
[morphClass:protected] =>
[exists] => 1
)
[1] => Gift Object
(
[table:protected] => gifts
[connection:protected] =>
[primaryKey:protected] => id
[perPage:protected] => 15
[incrementing] => 1
[timestamps] => 1
[attributes:protected] => Array
(
[id] => 3
[name] => Cherry Cake
[image] => Cherry-Cake-icon.png
[value] => 15
)
[original:protected] => Array
(
[id] => 3
[name] => Cherry Cake
[image] => Cherry-Cake-icon.png
[value] => 15
)
[relations:protected] => Array
(
)
[hidden:protected] => Array
(
)
[visible:protected] => Array
(
)
[appends:protected] => Array
(
)
[fillable:protected] => Array
(
)
[guarded:protected] => Array
(
[0] => *
)
[dates:protected] => Array
(
)
[touches:protected] => Array
(
)
[observables:protected] => Array
(
)
[with:protected] => Array
(
)
[morphClass:protected] =>
[exists] => 1
)
[2] => Gift Object
(
[table:protected] => gifts
[connection:protected] =>
[primaryKey:protected] => id
[perPage:protected] => 15
[incrementing] => 1
[timestamps] => 1
[attributes:protected] => Array
(
[id] => 4
[name] => Coconut
[image] => coconut-icon.png
[value] => 5
)
[original:protected] => Array
(
[id] => 4
[name] => Coconut
[image] => coconut-icon.png
[value] => 5
)
[relations:protected] => Array
(
)
[hidden:protected] => Array
(
)
[visible:protected] => Array
(
)
[appends:protected] => Array
(
)
[fillable:protected] => Array
(
)
[guarded:protected] => Array
(
[0] => *
)
[dates:protected] => Array
(
)
[touches:protected] => Array
(
)
[observables:protected] => Array
(
)
[with:protected] => Array
(
)
[morphClass:protected] =>
[exists] => 1
)
[3] => Gift Object
(
[table:protected] => gifts
[connection:protected] =>
[primaryKey:protected] => id
[perPage:protected] => 15
[incrementing] => 1
[timestamps] => 1
[attributes:protected] => Array
(
[id] => 5
[name] => Lemon
[image] => lemon-icon.png
[value] => 3
)
[original:protected] => Array
(
[id] => 5
[name] => Lemon
[image] => lemon-icon.png
[value] => 3
)
[relations:protected] => Array
(
)
[hidden:protected] => Array
(
)
[visible:protected] => Array
(
)
[appends:protected] => Array
(
)
[fillable:protected] => Array
(
)
[guarded:protected] => Array
(
[0] => *
)
[dates:protected] => Array
(
)
[touches:protected] => Array
(
)
[observables:protected] => Array
(
)
[with:protected] => Array
(
)
[morphClass:protected] =>
[exists] => 1
)
)
)
I believe this will do it for you:
Gift::whereIn('id', $giftImages)
->orderBy(DB::raw('FIELD(id, ' . implode(',', $giftImages) . ')'), 'ASC')
->get();
Taken from this question. You have to use DB::raw() since Laravel doesn't provide a native way to do this. Untested...