Laravel: Calling paginate() inside a blade? - php

I have access to a collection called $files inside my blade. I am paginating and setting it to $allFiles.
<?php $allFiles = $files->paginate(12); ?>
In the blade beneath that, when I call:
<div>
{{ $allFiles->links() }}
</div>
, I am getting the error call to a member function links() on array. I am not sure what I am doing wrong.
EDIT: Adding more info and show what $allFiles is. I logged it in my code and add that below:
Object
(
[collection:protected] => App\Models\Collection Object
(
[connection:protected] => data
[table:protected] => collections
[timestamps] => 1
[fillable:protected] => Array
(
[0] => title
[1] => content
)
[casts:protected] => Array
(
[smart_collection] => boolean
[is_app_collection] => boolean
)
[primaryKey:protected] => id
[keyType:protected] => int
[incrementing] => 1
[with:protected] => Array
(
)
[perPage:protected] => 15
[exists] => 1
[wasRecentlyCreated] =>
[attributes:protected] => Array
(
[id] => 2
[title] => Test
)
[original:protected] => Array
(
[id] => 2
[title] => Test
)
[dates:protected] => Array
(
)
[dateFormat:protected] =>
[appends:protected] => Array
(
)
[events:protected] => Array
(
)
[observables:protected] => Array
(
)
[relations:protected] => Array
(
)
[touches:protected] => Array
(
)
[hidden:protected] => Array
(
)
[visible:protected] => Array
(
)
[guarded:protected] => Array
(
[0] => *
)
)
[resultsCount:protected] => 19
[resultsProducts:protected] =>
[cache:protected] => Array
(
)
[splitOption:protected] =>
[context:protected] =>
)

Related

Attempt to read property "name" on bool

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) {
//....
}

Add new object key value to laravel db query results

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'

PHP custom sort on an object array Laravel-5

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

Laravel Foreach From Array

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'

How to keep order of collected database row the same as array passed to where clause

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...

Categories