in my system: a lead belongstomany salespeople a manager belongstomany salespeople i am trying to check if a lead has a manager through the salespeople. this is for a policy so i can make sure a manager can see the leads of their salespeople. something like this: is there a collection method that will allow me to do this? i’ve
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
Is there any way to optimize the query while working with 2mil+ rows database table
I am working on Laravel query that shall count the latest month data and also group by week, for the last 3 months. I have tried to fix that in a few ways but still it’s exceeds my memory limit and load very slow. Bellow is the current code that I am using to get the final results – but
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
Laravel collection multi level groupBy insert new items without checking each level’s key
Laravel collections now have a great multi-level groupBy function built in, but I am struggling with a sensible way to insert new items. For example: Very nice, easy to set up and access. For this example, I will assume each key is a number If there are already items in the nested positions [1], [2], and [3] then I can
Laravel get most recent array item for each day in array
I’m trying to figure out the best way (whether I do this using the query builder or raw PHP) to get the most recent array item based on it’s created_at timestamp for a given day, in an array …
Grouped collection to associative array
I need to achive array in format showed at the end, I’m using laravel collections $query->get()->groupBy(‘COLOR’)->toArray(); array:2 [ “GREEN” => array:123 [ 0 => array:…