Compare two columns of a collection in laravel? - laravel

I want to compare two columns of a collection.I don't want to do this in db level, only in collection level.
I have a collection like this (I returned it in array model for readability)
array:3 [
0 => array:5 [
"total_amount" => 200000.0
"admin_max_amount" => "200000000"
]
1 => array:5 [
"total_amount" => 100000.0
"admin_max_amount" => "200000000"
]
2 => array:5 [
"total_amount" => 100000.0
"admin_max_amount" => "0"
]
]
I want to get first of them where total_amount > admin_max_amount.

Here is a more 'Laravel' way to do it:
$items = collect([
[
"total_amount" => 200000.0,
"admin_max_amount" => "200000000",
],
[
"total_amount" => 100000.0,
"admin_max_amount" => "200000000",
],
[
"total_amount" => 100000.0,
"admin_max_amount" => "0",
],
]);
$result = $items->first(function ($item)
{
return (float)$item['total_amount'] > (float)$item['admin_max_amount'];
});
dump($result);

Related

Validating Arrays not working in laraval 9

Validating Arrays not working in laraval 9.
Request as follows
array:2 [
0 => array:4 [
"nic" => "908110248V"
"employee_id" => "1"
"request_id" => "2"
"schedule_training_id" => "1"
]
1 => array:4 [
"nic" => "962930898v"
"employee_id" => "2"
"request_id" => "1"
"schedule_training_id" => "1"
]
]
validator code snipit as follows
$validator = Validator::make($request->input('data_attributes'), [
'data_attributes.*.nic' => 'required|max:9'
]);
$validator = Validator::make($request->input('data_attributes'), [
'*.nic' => 'required|max:9'
]);
You should check the matrix as a whole and then start working with its elements, this is an example of what you should do:
$validator = Validator::make($request->input('data_attributes'), [
"data_attributes" => "required|array|min:3",
"data_attributes.*" => "required|string|distinct|min:3",
]);

Laravel collection for associative array

This is the array that I am trying to convert to collections and pluck the values.
$arr = [
"TID" => "81226042",
"TLineID" => "81226042",
"Sales" => [
[
"TLineID" => "226041"
],
[
"TLineID" => "226042"
],
[
"TLineID" => "9042"
]
]
];
//$r = collect($arr)->pluck('Sales');
$r = collect(json_decode(json_encode($arr)))->pluck('Sales');
print_r($r->toArray());
the output I get is
Array
(
[0] =>
[1] =>
[2] =>
)
I am expecting the 'Sales' Array here.
What am I doing wrong?
your doing wrong
$arr = [
[
"TID" => "81226042",
"TLineID" => "81226042",
"Sales" => [
[
"TLineID" => "226041"
],
[
"TLineID" => "226042"
],
[
"TLineID" => "9042"
]
]
],
[
"TID" => "81226042",
"TLineID" => "81226042",
"Sales" => [
[
"TLineID" => "226041"
],
[
"TLineID" => "226042"
],
[
"TLineID" => "9042"
]
]
],
];
$sales = collect($arr)->pluck('Sales')->toArray();
dd($sales);
i think this your trying to achieve

Remove duplicates in Laravel collection only if previous item is identical

I have a Laravel Collection of items:
[
[
'id' => 1,
'path' => '/',
'created_at' => '2020-05-20 20:12:00'
],
[
'id' => 2,
'path' => '/somewhere',
'created_at' => '2020-05-20 21:01:00'
],
[
'id' => 3,
'path' => '/somewhere',
'created_at' => '2020-05-20 21:21:00'
],
[
'id' => 4,
'path' => '/somewhere/else',
'created_at' => '2020-05-20 21:09:00'
],
[
'id' => 5,
'path' => '/somewhere/else',
'created_at' => '2020-05-20 21:10:00'
],
]
This is the raw data that would be found in the collection if I cast it to an array, but I need it to remain in collection format.
I need to remove any duplicates where the current item has the same path and is less than 5 minutes old compared to the previous item.
So in this case, item #4 would be removed leaving only #5, but items #2 and #3 would be kept even tho their paths are the same.
How would I do this?
Use the following namespace in your controller file:
use DateTime;
Use the following code in your controller file:
$collection = [
[
'id' => 1,
'path' => '/',
'created_at' => '2020-05-20 20:12:00'
],
[
'id' => 2,
'path' => '/somewhere',
'created_at' => '2020-05-20 21:01:00'
],
[
'id' => 3,
'path' => '/somewhere',
'created_at' => '2020-05-20 21:21:00'
],
[
'id' => 4,
'path' => '/somewhere/else',
'created_at' => '2020-05-20 21:09:00'
],
[
'id' => 5,
'path' => '/somewhere/else',
'created_at' => '2020-05-20 21:10:00'
],
];
$paths = [];
$time_count = [];
foreach ($collection as $key => $data) {
if (in_array($data['path'], $paths)) {
$arr_pos = array_search($data['path'], array_values($paths));
$datetime1 = new DateTime($data['created_at']);
$datetime2 = new DateTime($collection[$arr_pos]['created_at']);
$interval = $datetime1->diff($datetime2);
$mins = $interval->format('%i');
if ($mins < 5) {
unset($collection[$arr_pos]);
}
}
$paths[] = $data['path'];
}
This will return result:
array:4 [▼
0 => array:3 [▼
"id" => 1
"path" => "/"
"created_at" => "2020-05-20 20:12:00"
]
1 => array:3 [▼
"id" => 2
"path" => "/somewhere"
"created_at" => "2020-05-20 21:01:00"
]
2 => array:3 [▼
"id" => 3
"path" => "/somewhere"
"created_at" => "2020-05-20 21:21:00"
]
4 => array:3 [▼
"id" => 5
"path" => "/somewhere/else"
"created_at" => "2020-05-20 21:10:00"
]
]
I hope this works for you.

How to combine two collections by matching keys and their values

So, I've been bashing my head against this for a while and I cant get it to just do what I need it to.
I want to be able to store a set of valuse in our database as Json, but no matter what I cant get Laravel or format the object quite right first.
I've got three queries, One gets users usernames and their First name, as an object. Another two use that object to get a count of each users sales, and quotes, from our databsae with count().
Now all I need is the objects to be combined.
array:1 [
"" => array:3 [
0 => array:7 [
0 => array:2 [
"CallCentreID" => "sdickerson"
"FirstName" => "Sarah"
]
1 => array:2 [
"CallCentreID" => "wjones"
"FirstName" => "Wendy"
]
2 => array:2 [
"CallCentreID" => "aknox"
"FirstName" => "Alex"
]
3 => array:2 [
"CallCentreID" => "mking"
"FirstName" => "Melissa"
]
4 => array:2 [
"CallCentreID" => "nrowecc"
"FirstName" => "Neil"
]
5 => array:2 [
"CallCentreID" => "ejones"
"FirstName" => "Emma"
]
6 => array:2 [
"CallCentreID" => "spurnell2"
"FirstName" => "Simon"
]
]
1 => array:5 [
0 => array:2 [
"CallCentreID" => "aknox"
"Sales" => 1169
]
1 => array:2 [
"CallCentreID" => "ejones"
"Sales" => 401
]
2 => array:2 [
"CallCentreID" => "mking"
"Sales" => 767
]
3 => array:2 [
"CallCentreID" => "sdickerson"
"Sales" => 1067
]
4 => array:2 [
"CallCentreID" => "wjones"
"Sales" => 716
]
]
2 => array:6 [
0 => array:2 [
"CallCentreID" => "aknox"
"Quotes" => 3587
]
1 => array:2 [
"CallCentreID" => "ejones"
"Quotes" => 400
]
2 => array:2 [
"CallCentreID" => "mking"
"Quotes" => 6975
]
3 => array:2 [
"CallCentreID" => "nrowecc"
"Quotes" => 3
]
4 => array:2 [
"CallCentreID" => "sdickerson"
"Quotes" => 2686
]
5 => array:2 [
"CallCentreID" => "wjones"
"Quotes" => 2734
]
]
]
]
The above is as close as I can get with the code I have which is;
$Users = User::where('Accesslevel', 'laravelcallcentre')->select('EmailAddress as CallCentreID', 'FirstName')->get();
$Sales = Order::groupBy('CallCentreID')
// ->whereDate('OrderDate', '=', date('2018-06-21'))
->whereIn('CallCentreID', $Users->pluck('CallCentreID'))
->where('Status', 'BOOKING')
->selectRaw('CallCentreID, count( * ) AS Sales')
->get();
$Quotes = Order::groupBy('CallCentreID')
// ->whereDate('OrderDate', '=', date('2018-06-21'))
->whereIn('CallCentreID', $Users->pluck('CallCentreID'))
->where('Status', 'QUOTE')
->selectRaw('CallCentreID, count( * ) AS Quotes')
->get();
$collection = collect([$Users, $Sales, $Quotes]);
$CCData = $collection->groupBy('CallCentreID');
Anyone got any better Ideas? I feel like im doing this completley wrong. I had this written out as a foreach loop but that ended up being four queries for every user, whihc stacked up fast.
$sales = $sales->keyBy('CallCentreID');
$quotes = $quotes->keyBy('CallCentreID');
$users = $users->map(function($user) use($sales,$quotes) {
$user->sales = isset($sales[$user['CallCentreID']]['sales']) ? $sales[$user['CallCentreID']]['sales'] : 0;
$user->quotes = isset($quotes[$user['CallCentreID']]['quotes']) ? $quotes[$user['CallCentreID']]['quotes'] : 0;
return $user;
});

Laravel multiple records update with Eloquent

I want to Update multiple records to the reference table. Here is the response of the $request.
array:21 [
"_method" => "PATCH"
"_token" => "xXukmVEsMvyeBODIURqFx9Mhk4LviYrV6iLmAuOY"
"account_type" => "0"
"name" => "test"
"model_id" => "2"
"location" => "USA"
"serial_no" => "00055555"
"status" => "Sales"
"capacity_lower" => ""
"weight" => "1212.000"
"category" => "1"
"attribute" => array:3 [
1 => array:1 [
1 => "1"
]
3 => array:2 [
3 => "5"
2 => "2"
]
4 => array:5 [
5 => "11"
6 => "13"
4 => "9"
7 => "23"
8 => "37"
]
]
"crane_manufacture_id" => "1"
"condition" => "Average"
"manufacture_year" => "2020"
"unit_no" => "222222"
"hours" => "100"
"video_url" => ""
"sub_category" => "4"
"description" => "dsfs"
"productImages" => array:7 [
0 => array:3 [
"id" => "27"
"descripton" => "89d9a500-a936-4510-9ca5-5952ee9c0bff.jpg"
"status_id" => "2"
]
1 => array:3 [
"id" => "28"
"descripton" => "89d9a500-a936-4510-9ca5-5952ee9c0bff.jpg"
"status_id" => "2"
]
2 => array:3 [
"id" => "29"
"descripton" => "89d9a500-a936-4510-9ca5-5952ee9c0bff.jpg"
"status_id" => "2"
]
3 => array:3 [
"id" => "30"
"descripton" => "89d9a500-a936-4510-9ca5-5952ee9c0bff.jpg"
"status_id" => "2"
]
4 => array:3 [
"id" => "31"
"descripton" => "89d9a500-a936-4510-9ca5-5952ee9c0bff.jpg"
"status_id" => "2"
]
5 => array:3 [
"id" => "32"
"descripton" => "89d9a500-a936-4510-9ca5-5952ee9c0bff.jpg"
"status_id" => "2"
]
6 => array:3 [
"id" => "33"
"descripton" => "89d9a500-a936-4510-9ca5-5952ee9c0bff.jpg"
"status_id" => "2"
]
]
]
I have two tables, Products and product_images. I have given relation that one product has multiple images. I have apply hasMany relationship in both table.
Now at the time of Update record I am follow the below code for updating product_images records.
DB::enableQueryLog();
$product = Product::find($id);
//$product->save($request->input());
$product->productImages()->update(new ProductImage($request->input('productImages')));
dd(DB::getQueryLog());
But I am facing error with "MassAssignmentException in Model.php line 452:
0"
Check your model both products and product_images has fillable property. refer docs
protected $fillable = ['list of columns to be inserted'];
Check the fillable property protected $fillable = []; or use for all properties from the $request protected $guarded = [];

Resources