Skip to content

Tag: collections

Eloquent Sort By depend on the field

I have a query like this: I want to sort the result but i have problem. if the result of withMin if null i want to sort it by another field (lets say ‘product.sale_price’). Or how can i make a new field, lets say ‘final_price’, if promotion_price is null use sale_price. What i already tried: add this after get() the

How to display pagination links in a blade on Laravel 8?

I need to add pagination, but it seems complicated in my case, because I get data from database with paginate, but then I modify this data and when I call links() method on the blade, I get the following exception Method IlluminateSupportCollection::links does not exist. My code in the Controller method: Yes, a pagination limits my data to 10 rows,

How to add array inside array based on PHP condition?

I have a laravel collection below from: $unitinventory = DB::select(‘call wh_inventory_list(?)’, array(‘Units’)); And another collection: $modifification = collect(DB::table(‘pd_jo_bodymodification’)->get()); I want to find out if a $unitinventory->chassis_no has a modification history so I did this: Which returns the following: Now I want to add the conversion history to $unitinventory collection to look like this: How to make it possible? Thanks! Answer

Transform nested collection in laravel

I have a nested collection that I want to transform, pulling some keys “up a level” and discarding some other keys. Every item in the collection has an allergens property. I need to make each items allergens property, look like I’ve tried the following: But it doesn’t overwrite the allergens property Answer Since you’re posting your collection as JSON, I

Detect if array contains another array

I’m using external API for my website. When user select city and town, API returns neighborhoods. But the problem is, if there is only one neighborhood array contains name and neighborhood id. But if there is more than one, it contains multiple array for each neighborhood. Single example ; Multiple Example ) I have to show data to user inside