I have a collection of Objects which take the following form
#attributes: array:8 [▼
"name" => "Something1"
"value" => "Some Data"
]
#attributes: array:8 [▼
"name" => "Something1Test"
"value" => "Some Data"
]
#attributes: array:8 [▼
"name" => "NOC M2"
"value" => "Some Data"
]
#attributes: array:8 [▼
"name" => "Couldbeanything M3"
"value" => "Some Data"
]
#attributes: array:8 [▼
"name" => "Couldbeanything M3Test"
"value" => "Some Data"
]
If a name attribute ends with Test, the preceding Object is related to it. So for instance Something1Test is related to Something1. The first part will always be the same "Something1", the cloned version just has Test added to the end.
I am looping my Objects and displaying their content. What I am trying to do is display a checkbox for all Objects that do not have a related Test Object. At the moment I have something like this
<?php if(strpos($data->name, 'Test') === false) { ?>
<div class="col-md-4">
//Display a checkbox
</div>
<?php } ?>
Now this somewhat works in a sense that it displays a checkbox for all Objects that dont have a name attribute which contains Test. So for the above data, it is displaying a checkbox for
Something1
NOC M2
Couldbeanything M3
The problem I am having is that Something1 and Couldbeanything have a related Test Object, so these two should not have a checkbox. With the above data, the only thing that should have a checkbox is NOC M2, because there is no NOC M2Test.
Is there any way to achieve what I am after? The first part of the name could be anything.
Thanks
I would first gather the names:
$names = array_column($objects, 'name');
The above assumes a nested array structure; you may need to do something like this instead:
$names = array();
foreach ($objects as $data) {
$names[] = $data->name;
}
Then I would perform the following check:
// Must not end in Test and must not have a corresponding Test object
if (substr($data->name, -4) != 'Test' && !in_array($data->name . 'Test', $names)) {
// do my output here
}
Related
I am new in laravel and working on E-Commerce Project.
Plz Help me!
I am trying to get data from the database but I can't.
My function
public function add_product_attributes($id)
{
$attributes = Product::where(['id' => $id])->with('attributes')->first();
$attributes_data = $attributes->toArray();
dd($attributes_data); //here i can see my required data
$product_attributes = product::find($id);
// dd($product_attributess);
return view('admin.add_product_attributes', compact('product_attributes', 'attributes_data'));
}
// I want to get multiple records like SKU, Color, Size, etc
//Here this is a record that I can see using dd($attributes_data)
array:14 [▼ // app\Http\Controllers\AdminController.php:228
"id" => 2
"title" => "shirt"
"category_id" => 1
"price" => "12$"
"dis_price" => "1000$"
"quantity" => "1"
"product_code" => "002"
"product_color" => "blue"
"short_description" => "test"
"long_description" => "test"
"image" => "1670482697.raza.jpeg"
"created_at" => "2022-12-08T06:58:17.000000Z"
"updated_at" => "2022-12-08T06:58:17.000000Z"
"attributes" => array:4 [▼
0 => array:8 [▶]
1 => array:8 [▶]
2 => array:8 [▶]
3 => array:8 [▶]
]
]
At this point, I think everything is fine
//I can see my required data but I can't show that in the view file in foreach loop. It gives me an error Attempt to read property "id" and all data like category_id, SKU etc on an integer
Here I am pasting the view file details
#foreach ($attributes_data as $data)
<tr>
<th scope="row">{{$data->id}}</th>
<td>{{$data->category_id}}</td>
<td>{{$data->product_id}}</td>
<td>{{$data->sku}}</td>
<td>{{$data->size}}</td>
<td>{{$data->price}}</td>
<td>{{$data->stock}}</td>
</tr>
#endforeach`
I want to resolve this error and show all attributes of my single product.
plz help me I will be very thankful to you
If I understood the purpose of your codes well
You want to access a product + its attributes in the view
I tried to make your code a little cleaner and more understandable
(I hope I understand your question correctly so that my answer is useful for us)
public function add_product_attributes($id)
{
$product = Product::where('id' , $id)->with('attributes')->first();
$attributes = $product->attributes;
return view('admin.add_product_attributes', compact('product', 'attributes' ));
}
view
#foreach ($attributes as $attribute)
<tr>
<th scope="row">{{attribute->id}}</th>
// ...
</tr>
#endforeach
You're transforming your attributes to an array. But you're fetching only one record so you don't need to wrap it in foreach loop here. You can simply do this.
<tr>
<th scope="row">{{$attributes_data['id']}}</th>
<td>{{$attributes_data['category_id']}}</td>
<td>{{$attributes_data['product_id']}}</td>
<td>....</td>
</tr>
I'm trying to insert some records into a dabase using Laravel's Eloquent ORM.
The data is stored in an array which looks like this (there are more than one elements in the array)
The initial array which I'm looping through looks like this
array:1 [▼
"Product" => array:4416 [▼
0 => array:9 [ …9]
1 => array:9 [ …9]
2 => array:9 [ …9]
3 => array:10 [ …10]
---------------------------------------------------------------------
array:9 [▼
"ProductCode" => "EWS1025CAM"
"Vendor" => "ENGENIUS"
"ProductType" => "Security - Surveillance Indoor Camera"
"ProductCategory" => "Other"
"ProductDescription" => "Managed AP-CAM Indoor Dual Band 11ac 2T2R 300+867Mbps 2MP dome 4mm IR20m PoE.af μSDHC (Access point - Camera, Power Adapter (12V/1.5A), T-rail mounting kit, mou ▶"
"Image" => "https://www.it4profit.com/catalogimg/wic/1/EWS1025CAM"
"ProductCard" => "https://content.it4profit.com/itshop/itemcard_cs.jsp?ITEM=171208071213009310&THEME=asbis&LANG=ro"
"AttrList" => array:1 [▶]
"Images" => array:1 [▶]
]
I'm looping through it and inserting values like this
foreach ($phpDataArray['Product'] as $key => $value)
{
$insert = new FurnizorProduse();
$insert->product_id = $value['ProductCode'];
$insert->product_data = json_encode($value);
$insert->date = new \DateTime();
$insert->module_id = 58;
$insert->save();
}
Even though the data is inserted correctly into the db, I get "Argument 1 passed to Illuminate\Database\Grammar::parameterize() must be of the type array, string given".
The line of code causing this is $insert->product_id = $value['ProductCode'];
as if I replace the $value['ProductCode']; wiht an integer let's say, no more errors.
Any ideas ? Thanks !
In one of your iteration of:
$phpDataArray['Product'], you don't have: $value['ProductCode'], it's my assumption.
Try to do:
if(is_array($value) && isset($value['ProductCode'])) {
$insert = new FurnizorProduse();
...
}
I have some input fields in my blade that looks like this:
<input type="text" name="product[0][name]">
<input type="text" name="product[0][price]">
<input type="text" name="product[1][name]">
<input type="text" name="product[1][price]">
if I submit the form, I get this back if I dd(Input::get("product)):
MY PRODUCT ARRAY
array:2 [▼
0 => array:2 [▼
"name" => "unicorn"
"price" => 5000
]
1 => array:2 [▼
"name" => "house"
"price" => 10000
]
]
Now I have another array, that looks like this:
API PRODUCT ARRAY
array:80 [▼
0 => array:18 [▼
"name" => "john doe for sale"
"price" => 120
...
]
1 => array:18 [▼
"name" => "house"
"price" => 12000
..
]
3 => array:18 [▼
"name" => "unicorn"
"price" => 5100
...
]
4 => array:18 [▼
"name" => "blabla"
"price" => 60000
...
]
And now my problem
I want to check if in "My Product Array" are products with the same name, like in the "API Product Array"
And if this should be the case, then it should be checked if the price of one of the products from the "API Products Array" is higher than that of the "My Products Array".
If this happens, this product should be added to a UserNotificationProducts array.
The code I made looks like this, but haven't worked for me at all.
Here I need some help.
foreach ($apiproducts as $key => $product)
{
if (in_array($product["name"], $myproducts["name"]))
{
if($product["price"] > (the current product of "myproducts")
array_push($productnotification, $product["name"]);
}
}
The "My Product Array" have a maximum of three products. The API Products array up to 100.
Thanks!
I tried both answers out and calculated the time cost of both solutions.
The answer of Istiaque Ahmed needed 0.15902519226074 ms
The answer of Hamelraj ( I tried exactly what you did before you posted your answer :D ) needed 0.031232833862305 ms
So the answer of hamelraj is the quicker solution. Thanks both of you!
$productnotification = [];
foreach ($apiproducts as $api)
{
foreach ($myproduct as $product)
{
if($product['name'] == $api['name'] && $api['price'] > $product['price'])
array_push($productnotification, $product["name"]);
}
}
You wrote if (in_array($product["name"], $myproducts["name"])). But $myproducts is an array of arrays. So you do not get any name from there. The following code might help:
foreach ($apiproducts as $key => $product)
{
$product_name=$product["name"];
for($i=0; $i<count($myproducts); $i++){
if (strcasecmp($product_name,$myproducts[$i]['name']==0) ){
if($product["price"] > (the current product of "myproducts")
array_push($productnotification, $product["name"]);
}
}// end of for loop
}
Is there a way to filter a collection returned by database query on Laravel?
Returned collection is this
1 => Person {#310 ▼
#table: "person"
#attributes: array:15 [▼
"name" => "Jon Doe"
"timeavailable" => "1,2,3,4,5,6,7,8"
2 => Person {#310 ▼
#table: "person"
#attributes: array:15 [▼
"name" => "Jon Moe"
"timeavailable" => "1,2,5,7,8"
Here, I want to filter the timeavailable attribute. I only want to show results for 1,2,3 . Is there a way to filter this collection so that I only get something like this
1 => Person {#310 ▼
#table: "person"
#attributes: array:15 [▼
"name" => "Jon Doe"
"timeavailable" => "1,2,3"
2 => Person {#310 ▼
#table: "person"
#attributes: array:15 [▼
"name" => "Jon Moe"
"timeavailable" => "1,2"
So, the filtered collection will have only 1,2,3 inclusions.
I think this is not the best solution, but it can be a way to solve for this problem. Check if this helps...
Try to use the map of Collection. In the inner function, you can explode, intersect and explode timeavailable.
$limiter = [1,2,3];
$filtered = $array->map(function($person, $k) use ($limiter) {
$ex = explode(',', $person->timeavailable);
$intersect = array_intersect($limiter, $ex);
$person->timeavailable = implode(",",$intersect);
return $person;
});
This will work fine for small results, but if you are planning to run this with lots of results, it will be slow.
I have an array which looks like the following
array:2 [▼
0 => array:1 [▼
"input1" => "Something"
]
1 => array:1 [▼
"input2" => ""
]
]
Now the first element will always have some data. It is the second element I am interested in. At the moment, I am trying this
if(!empty($clientGroup[0][1]) || !empty($clientGroup[1][1]))
var_dump("Some Data");
} else {
var_dump("Both Empty");
}
The else should only be triggered if both elements are empty e.g.
array:2 [▼
0 => array:1 [▼
"input1" => ""
]
1 => array:1 [▼
"input2" => ""
]
]
If one of them have any data, the if should be triggered (so for the first array I showed, the if should be triggered).
How would I go about doing this, empty does not seem to work.
Thanks
The 2nd level keys do not exist so you will always be told the values are empty. Change the line
if(!empty($clientGroup[0][1]) || !empty($clientGroup[1][1]))
to,
if(!empty($clientGroup[0]['input1']) || !empty($clientGroup[1]['input2']))
and you should get the results you're after.
It's not realy 2D array because you have associative array inside an other array.
you must use key name (input1, input2) to access the value.
I recommend to use
if($retourdata[0]["input1"] !== "" || $retourdata[1]["input2"] !== "")