How to remove element from multidimensional cakephp session - session

I am working on eCommerce website where is stored all cart product in session that is working perfectly.
Here are debug of cart session.
debug($this->request->getsession()->read('cart'));
[
(int) 1 => [
(int) 0 => [
'id' => (int) 1,
'picture' => '1_1.webp',
'sku' => 'TH447WA38OUMINDFAS',
'name' => 'The Vanca Multicoloured Printed Strappy Top',
'size' => 'S',
'price' => '480'
]
],
(int) 2 => [
(int) 0 => [
'id' => (int) 2,
'picture' => '2_1.webp',
'sku' => 'AL384WA86QOSINDFAS',
'name' => 'All About You Pink Embroidered Blouse',
'size' => 'S',
'price' => '1330'
]
],
(int) 3 => [
(int) 0 => [
'id' => (int) 3,
'picture' => '3_1.webp',
'sku' => 'RE367WA35NDKINDFAS',
'name' => 'Renka Comfortable Black Color Seamless Summer Tops For Women',
'size' => 'S',
'price' => '495'
]
]
]
Now i want to remove any row from cart but that is not working for me.
unset($this->request->getsession()->read('cart')[1]);

should be simply
$this->request->getSession()->delete('cart.1');
you can use dot notation when accessing session arrays
you could also read and delete the data in one command
$cart = $this->request->getSession()->consume('cart');
see the manual here and the API here and here

Related

Best way to update a lot of data with difference value?

I have a lot of data and each record have difference value. I try to loop insert/update data but it take time. Is it have better way to improve my code?
I don't want to delete all data before update.
$data = [
[ 'id' => 1, 'name' => 'A' ],
[ 'id' => 2, 'name' => 'B' ],
[ 'id' => 3, 'name' => 'C' ],
[ 'id' => 4, 'name' => 'D' ],
.
.
.
[ 'id' => 10000, 'name' => 'ZYX' ],
]
foreach ($data as $item) {
DB::table('my_table')->updateOrInsert(['id' => $item->id], $item);
}
Thank.
You can use upsert in Laravel 8.x or use this package: https://github.com/staudenmeir/laravel-upsert to use INSERT ON DUPLICATE
$data = [
[ 'id' => 1, 'name' => 'A' ],
[ 'id' => 2, 'name' => 'B' ],
[ 'id' => 3, 'name' => 'C' ],
[ 'id' => 4, 'name' => 'D' ],
.
.
.
[ 'id' => 10000, 'name' => 'ZYX' ],
];
DB::table('my_table')->upsert($data, 'id');
use laravel upsert method here
for example you want to update if exists or insert record if not exists
$data = [
[
'id' => 1
'name' => 'J.K. Rowling',
'author' => 'Harry Potter',
'quantity' => 15
],
[
'id' => 2
'name' => 'Cal Newport',
'author' => 'Deep Work',
'quantity' => 20
]
];
DB::table('books')->upsert($data, ['id'], ['quantity']);
1.The first argument consists of the values to insert or update.
2.The second argument lists the column(s) that uniquely identify records (id in our case) within the associated table.
3.The third and final argument is an array of columns that should be updated if a matching record already exists in the database. We want the quantity to be updated in our case.
for more detail read official docs upsert

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.

Laravel validation: required field in not required array

Request can contain field coord ({x: 1, y: 2}) or not contain it. For example:
Correct (without coord):
[
'another_param' => 'value',
],
Correct:
[
'another_param' => 'value',
'coord' => [
'x' => 1,
'y' => 2,
],
],
Invalid (wrong format of coord):
[
'another_param' => 'value',
'coord' => [
'x' => 1,
],
],
Whether it can be written by standard rules (without custom and closures).
My attempt:
'rules' => [
'coord' => 'array',
'coord.x' => 'required',
'coord.y' => 'required',
],
But if a request don't contain coord then Error: The coord.x field is required.
You can use the sometimes validation rule so it only applies when the field is present.
'rules' => [
'coord' => 'sometimes|array',
'coord.x' => 'required',
'coord.y' => 'required',
],
You can require the fields when the array is provided using required_with:
'rules' => [
'coord' => 'sometimes|array|min:1',
'coord.x' => 'required_with:coord',
'coord.y' => 'required_with:coord',
],
The sometimes rule allows this field to be discarded and min:1 ensures when this coord field is provided, it's not an empty array.
You can try validating the array fields as coord.*.x
I mean:
'rules' => [
'coord' => 'array',
'coord.*.x' => 'required',
'coord.*.y' => 'required',
],
Then if coord array has an element, x and y will be required
Not sure if it's still actual but here's how I solved it:
'rules' => [
'coord' => ['nullable', 'array'],
'coord.x' => 'required_unless:coord,null',
'coord.y' => 'required_unless:coord,null',
],

cakephp-3 form validation for length range (between)

I am converting Cakephp-1 project to Cakephp-3. So I need help to convert the validation code:
Cakephp-1 Code
'name' => array(
'between' => array(
'rule' => array('between', 2, 50),
'message' => 'Ditt namn måste vara minst två tecken långt!'
)
)
I have try the code:
Cakephp-3 Code
$validator
->requirePresence('name', 'create')
->notEmpty('name')
->add('name', [
'between' => [
'rule' => [2, 50],
'message' => 'Namnet måste vara mellan 2 och 50 tecken lång!',
]
]);
But it gives me the error:
Unable to call method "2" in "default" provider for field "name"...
Now what can I do ??
The correct code should be
->add('name', [
[
'rule' => ['lengthbetween', 2, 50],
'message' => 'Namnet måste vara mellan 2 och 50 tecken lång!',
]
]);
You can also set different validation messages for each minimum length and maximum length:
$validator
->add('body', [
'minLength' => [
'rule' => ['minLength', 10],
'last' => true,
'message' => 'Comments must have a substantial body.'
],
'maxLength' => [
'rule' => ['maxLength', 250],
'message' => 'Comments cannot be too long.'
]
]);

Ignore Results Outside of Distance Range

I am working with ElasticSearch for an application which deals with "posts". I currently have it working with a geo_point so that it will return all posts ordered by distance from the end-user. While this is working I also need to work in one more aspect for the system.
Posts can be paid for and for instance if I were to pay for my post and choose "Local" as the area range then this post should only show to end-users which are less than or equal to 20 miles away.
I have a column on my index named spotlight_range, is there a way I can create a query to say ignore all records if the spotlight_range = 'Local' and the distance is > 20 miles? I need to do this for several different spotlight ranges. For instance Regional may be 100 miles or less, etc.
My current query looks like this
$params = [
'index' => 'my_index',
'type' => 'posts',
'size' => 25,
'from' => 0,
'body' => [
'sort' => [
'_geo_distance' => [
'post_location' => [
'lat' => '44.4759',
'lon' => '-73.2121'
],
'order' => 'asc',
'unit' => 'mi'
]
],
'query' => [
'filtered' => [
'query' => [
'match_all' => []
],
'filter' => [
'geo_distance' => [
'distance' => '100mi',
'post_location' => [
'lat' => '44.4759',
'lon' => '-73.2121'
]
]
]
]
]
]
];
My index is setup with the following fields.
'id' => ['type' => 'integer'],
'title' => ['type' => 'string'],
'description' => ['type' => 'string'],
'price' => ['type' => 'integer'],
'shippable' => ['type' => 'boolean'],
'username' => ['type' => 'string'],
'post_location' => ['type' => 'geo_point'],
'post_location_string' => ['type' => 'string'],
'is_spotlight' => ['type' => 'boolean'],
'spotlight_range' => ['type' => 'string'],
'created_at' => ['type' => 'date', 'format' => 'yyyy-MM-dd HH:mm:ss'],
'updated_at' => ['type' => 'date', 'format' => 'yyyy-MM-dd HH:mm:ss']
My end goal for this is not specifically to search for distance < X and range = Y but rather to have it filter them out for all types based on distances I specify. The search should return ALL types of ranges but also filter out anything past my specified distance for each range type based on the users lat/lon passed into the query.
I have been looking for a solution to this online without much luck.
I would add a circle geo_shape to the document, centered on post_location and with a radius corresponding to the spotlight_range since you know both information at indexing time. That way you can encode into each post its corresponding "reach".
...
'post_location' => ['type' => 'geo_point'],
'spotlight_range' => ['type' => 'string'],
'reach' => ['type' => 'geo_shape'], <---- add this
So a "local" document would look something like this once indexed
{
"spotlight_range": "local",
"post_location": {
"lat": 42.1526,
"lon": -71.7378
},
"reach" : {
"type" : "circle",
"coordinates" : [-71.7378, 42.1526],
"radius" : "20mi"
}
}
Then the query would feature another geo_shape centered on the user's location with the chosen radius and would only retrieve documents whose reach intersects the circle shape in the query.
$params = [
'index' => 'my_index',
'type' => 'posts',
'size' => 25,
'from' => 0,
'body' => [
'sort' => [
'_geo_distance' => [
'post_location' => [
'lat' => '44.4759',
'lon' => '-73.2121'
],
'order' => 'asc',
'unit' => 'mi'
]
],
'query' => [
'filtered' => [
'query' => [
'match_all' => []
],
'filter' => [
'geo_shape' => [
'reach' => [
'relation' => 'INTERSECTS',
'shape' => [
'type' => 'circle',
'coordinates' => [-73.2121, 44.4759],
'radius' => '20mi'
]
]
]
]
]
]
]
];

Resources