Laravel Collections search - laravel

I have SQL query & sub query results and then convert these results to collection as bellow
$co=collect($users);
my collection array is as bellow
Illuminate\Support\Collection^ {#1984
#items: array:4 [
"id" => 92
"user" => "abc"
"created_at" => "2020-04-16T12:13:11.000000Z"
"updated_at" => "2020-12-05T20:32:56.000000Z"
"groups" => array:6 [
0 => array:6 [
"id" => 1
"user_id" => 1
"group_id" => 92
"title" => "new group"
"created_at" => "2020-04-16T12:13:11.000000Z"
"updated_at" => "2020-12-05T20:32:56.000000Z"
1 => array:6 [
"id" => 1
"user_id" => 1
"group_id" => 91
"title" => "other group"
"created_at" => "2020-04-16T12:13:11.000000Z"
"updated_at" => "2020-12-05T20:32:56.000000Z"
as you can see there is two columns with name of {id} , one in first array and other in sub array "groups"
I want to search the collection for ID and for title
I am using these command but both are returning empty array.
$filtered = $co->where('id',1);
dd($filtered);
also
$filtered = $co->where('title','new group');
dd($filtered)
;
or
$filtered = $co->where('groups.title','new group');
dd($filtered);
how to get these values
Thanks

Related

How to use whereIn() when column names don't match?

I am trying to use whereIn() to find records who have 'prov' that can be found in this array;
array:3 [▼
0 => array:6 [▼
"id" => 1
"contest_id" => 1
"contest_zone_id" => 1
"province" => "ON"
"created_at" => "2018-06-20 13:37:45"
"updated_at" => "2018-06-20 13:37:45"
]
1 => array:6 [▼
"id" => 2
"contest_id" => 1
"contest_zone_id" => 1
"province" => "SK"
"created_at" => "2018-06-20 13:37:45"
"updated_at" => "2018-06-20 13:37:45"
]
2 => array:6 [▼
"id" => 3
"contest_id" => 1
"contest_zone_id" => 1
"province" => "NB"
"created_at" => "2018-06-20 13:37:45"
"updated_at" => "2018-06-20 13:37:45"
]
]
So I can do this;
->whereIn('prov', $provinces)
However, in my 2 tables 'prov' and 'province' aren't the same so it's not working. Is there a way to explicitly declare which columns I want to compare?
Thanks!
UPDATE WITH FIX
Array needs to be pared down to just the field you are looking to match.

Laravel Eloquent eager Loading and Retrieving Values From the Array

Using Laravel's Eloquent and eager loading how would one retrieve the values in the built_load_cost array and th built_load_revenue array?
```
array:11 [▼
"id" => 5
"created_at" => "2018-03-24 00:19:52"
"updated_at" => "2018-03-24 00:20:14"
"load_number" => 1
"dispatcher_id" => 3
"carrier_id" => 826097731
"customer_id" => 1
"ip" => "127.0.0.1"
"deleted_at" => null
"built_load_cost" => array:6 [▼
"id" => 4
"created_at" => "2018-03-26 19:59:34"
"updated_at" => "2018-03-26 19:59:34"
"line_haul" => "100.00"
"extra_pickups" => "0.00"
"built_load_id" => 5
]
"built_load_revenue" => array:6 [▼
"id" => 4
"created_at" => "2018-03-26 19:59:43"
"updated_at" => "2018-03-26 19:59:43"
"line_haul" => "90.00"
"extra_pickups" => "0.00"
"built_load_id" => 5
]
]
```
When accessing value through eloquent model, use camelCase, with first lower case letter:
$load->builtLoadCost->line_haul
$load['builtLoadCost']['line_haul']
If the eloquent model is converted to array, use snake_case to access relations:
$load->toArray()['built_load_revenue']['line_haul']
See also: Laravel “with” changes variable case to snake case

How to groupby() and sortby() a Laravel collection?

This is the structure of my collection
0 => array:13 [▼
"id" => 173
"campaign_id" => "STM1234"
"product_id" => "STM1234"
"group" => "24"
"version" => "1"
"created_at" => "2017-08-04 14:02:33"
"updated_at" => "2017-08-04 14:02:33"
"groups" => array:5 [▼
"id" => 24
"campaign_id" => "STM1234"
"group_name" => "default"
"updated_at" => null
I am trying to group and order this collection.
$groups = $products->groupBy('groups.group_name')->sortBy('groups.id');
The grouping works however the ordering does not work
array:2 [▼
"group2" => array:3 [▶]
"default" => array:8 [▶]
]
Default has an id of zero so should be first what do I need to do to get the group with the id of 0 first on the list?

How to get a particular field value from multidimensional array in laravel

I have a variable $cart which stores the details of product. I want to get a particular field from the cart.
dd($cart) show the following result.
Cart {#437 ▼
+items: array:1 [▼
"airports_334_64" => array:4 [▼
"qty" => 1
"price" => 1600000
"duration" => 0
"item" => array:28 [▼
"id" => 64
"created_at" => "2017-05-29 10:24:22"
"updated_at" => "2017-05-29 10:26:51"
"title" => "Airport ad"
"price" => "120000"
"location" => "Airport T3"
"city" => "Delhi"
"state" => "Delhi"
"rank" => "12"
"landmark" => "abc"
"description" => "<p>new</p>"
"image" => "1496053462.jpg"
"references" => ""
"status" => "Available"
"display_options" => null
"light_option" => null
"airportnumber" => null
"discount" => "1"
"slug" => null
"reference_mail" => "chingkhei91#gmail.com"
"airports_id" => "64"
"area" => "arrival_check_in_hall"
"displayoption" => "backlit_panel"
"dimensions" => "7'10"x3'9""
"optionprice" => "1600000"
"units" => "8"
"ad_code" => ""
"variation_id" => 334
]
]
]
+totalQty: 1
+totalPrice: 1600000
}
I want to get the value of reference_mail i.e Cart->items->item->reference_mail
Use the Collections provided by Laravel:
$items = collect($cart->items);
$reference_mails = $items->map(function($item){
return $item['reference_mail'];
});
This leaves you with an array of all the reference mails. The behaviour of map is described here
If you want to access them then you should iterate over the items like so.
foreach ($cart->items as $items) {
foreach ($items as $item) {
$email = $item['item']['reference_mail'];
}
}
If you need to access it directly based on array index then you could do this.
$email = $cart->items['airports_334_64']['item']['reference_mail'];
If you need the list of reference_mail then you do this in your query rather than going through all this. If you have a collection then you can use pluck method to retrieve the list of emails.

How to check if multidimensional array is empty or not in laravel

I'm trying to check if there is an empty array in the nested arrays.
This is what I get from my form.
array:15 [▼
"_token" => "h4aR4xJlWhZveRKbAgHzgzHWSKSqyhVKb7OHAgWH"
"name" => "Test office"
"is_department" => "0"
"hours" => "1-3"
"description" => "Description"
"content" => "<p>Content</p>"
"street" => "123 Street"
"city" => "Foomania"
"state" => "Sweet state"
"postal" => "98234"
"phone" => "5748293212"
"fax" => "2123131233"
"email" => "test#domain.tld"
"additional-page" => ""
"office_fees" => array:4 [▼
0 => array:2 [▼
"description" => ""
"fee" => ""
]
1 => array:2 [▼
"description" => ""
"fee" => ""
]
2 => array:2 [▼
"description" => ""
"fee" => ""
]
3 => array:2 [▼
"description" => ""
"fee" => ""
]
]
]
How can I check if there is empty array in office_fees ?
Just to be clear, office_fees will always return at least one array. What I'm trying to is to be able to determine whether the office_fees need to be saved into another model.
Not sure what You're looking for but:
empty($data['office_fees'])
checks if an array isset and not empty. If You want to check an empty array try this:
if (is_array($data['office_fees']) && !empty($data['office_fees']))
In laravel 5.2 you can validate arrays
$validator = Validator::make($request->all(), [
'office_fees.*.description' => 'required',
]);
Source: Laravel documentation

Resources