Attempt to read property "id" on integer - php

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>

Related

undefined index 'value' in laravel

im using laravel 7 and trying to get datas from api use http.
when i use dd('$datas'),i got this
array:1 [▼"manajemen_sdm" => array:4 [▼ 0 => array:9 [▼ "id_karyawan" => "2" "nama" => "Koko Hendriko" "gender" => "Laki-Laki" "ttl" => "1998-04-07" "alamat" => "Kp.Palasari " "no_telp" => "821863141" "email" => "kokohendriko#gmail.com" "npwp" => "89678923652" "golongan" => "B" ] 1 => array:9 [▶] 2 => array:9 [▶] 3 => array:9 [▶] ] ]
my controller
public function index()
{
$datas = Http::get('https://projectsoadenis.000webhostapp.com/api')->json();
return view('sdm.index',compact('datas'));
}
view
#foreach ($datas as $datasdm)
<tr>
<td>{{$no++}}</td>
<td>{{$datasdm['id_karyawan']}}</td>
<td>{{$datasdm['nama']}}</td>
<td>{{$datasdm['gender']}}</td>
<td>{{$datasdm['ttl']}}</td>
<td>{{$datasdm['alamat']}}</td>
<td>{{$datasdm['no_telp']}}</td>
<td>{{$datasdm['email']}}</td>
<td>{{$datasdm['npwp']}}</td>
<td>{{$datasdm['golongan']}}</td>
</tr>
#endforeach
If you look at the data you are getting back from the API, your data is actually nested inside objects that live under a manajemen_sdm property.
Your loop should work correctly if you update it to loop over the manajemen_sdm key:
#foreach ($datas['manajemen_sdm'] as $datasdm)
<tr>
<td>{{$no++}}</td>
<td>{{$datasdm['id_karyawan']}}</td>
<td>{{$datasdm['nama']}}</td>
<td>{{$datasdm['gender']}}</td>
<td>{{$datasdm['ttl']}}</td>
<td>{{$datasdm['alamat']}}</td>
<td>{{$datasdm['no_telp']}}</td>
<td>{{$datasdm['email']}}</td>
<td>{{$datasdm['npwp']}}</td>
<td>{{$datasdm['golongan']}}</td>
</tr>
#endforeach

Laravel swap the position in collection - multidimensional array

Following is the array in the collection:
array:1 [▼
"online" => array:2 [▼
"IS-003" => array:19 [▼
"product" => Product {#831 ▶}
"quantity" => 1
"payment-option" => "online"
]
"HA4" => array:19 [▼
"product" => Product {#822 ▶}
"quantity" => 1
"payment-option" => "online"
]
]
]
Whenever a user changes the payment-option, accordingly the above array should change.
For instance, if HA4['payment-option'] is changed from online to cod, then there should be 2 arrays in parent array.
Following is the array that I want as result.
array:2 [▼
"online" => array:1 [▼
"IS-003" => array:19 [▼
"product" => Product {#831 ▶}
"quantity" => 1
"payment-option" => "online"
]
]
"cod" => array:1 [▼
"HA4" => array:19 [▼
"product" => Product {#822 ▶}
"quantity" => 1
"payment-option" => "cod"
]
]
]
The thing that I have tried so far but couldn't get the desired result:
$paymentOptionCart = collect();
foreach ($cart as $paymentType => &$details) {
foreach ($details as $c => $p) {
if ($c == $code) {
$details[$c]['payment-option'] = $request->option;
$paymentOptionCart->put($paymentType, $details);
unset($details[$c]);
}
}
}
On executing the above code, nothing happens except the payment-option is updated to cod.
I know I am making a silly mistake somewhere, but I am unable to locate where and how.
Can anybody help me out?
This should do your task:
$array = [
"online" => [
"IS-003" => [
"quantity" => 1,
"payment-option" => "online"
],
"HA4" => [
"quantity" => 1,
"payment-option" => "online"
]
]
];
$code = "HA4";
$request_option = "cod";
foreach ($array as $paymentType => $details) {
foreach ($details as $c => $p) {
if ($c == $code) {
$array[$request_option][$c] = $p;
$array[$request_option][$c]["payment-option"] = $request_option;
unset($array[$paymentType][$c]);
}
}
}
unset($details[$c]) seems to be the problem... this punches out the element at index 0, at the end of the first iteration - and index 1 should then become index 0, which subsequently will not be accessible at index 1, during the next iteration of the loop. just run the loop until the exit condition is met and then unset them all, not during the loop... or loop backwards, in order to keep the indexes intact; this would unset the last one element at first and the loop would not exceed the array boundaries.
having two different payment options within a single online order is rather an unlikely example, haven't seen this yet. people might rather post two orders (which implies, not only the business logic is flawed, but the array structure has an unfortunate design, which requires such messing around).
xdebug is great for understanding what is happening. even if my answer might not answer the question in code one can copy & paste (that's not my job), xdebug will tell you exactly what the problem is.

Laravel - Check comepare values in to arrays and check if price is higher

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
}

PHP - Displaying checkbox based on certain conditions

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
}

#foreach not looping array as expected

I'm trying to loop through an array provided from facebook graph api. The initial array holds two fields (data, and paging). The data field is also an array filled with 50+ items.
array:2 [▼
"data" => array:77 [▶]
"paging" => array:2 [▶]
]
and opened:
array:2 [▼
"data" => array:77 [▼
0 => array:5 [▼
"id" => "238123092879087er098234_28365"
"picture" => "https://scontent.xx.fbcdn.net/hphotos-xft1/v/t1.0-0/s130x130/12235138_10we2133658asdasdfafsasf5816390956_3188793651778686071_n.jpg?oh=eb8f907a1e8df5efe99cb2b9fafa9c05&oe=56E5627B"
"message" => """
some new GIN available # TasTToe!!\n
\n
MASCARO 9 GIN 40°\n
Alkkemist GIN \n
ATOMIC GIN 40° FROM AtomicDistillers\n
Elephant Gin\n
PLATU LONDON DRY GIN 39°
"""
"link" => "https://www.facebook.com/2381232313223159570563/photos/a.238553749527504.54095.238123159570563/1036585816390956/?type=3"
"full_picture" => "https://scontent.xx.fbcdn.net/hphotos-xft1/v/t1.0-9/12235138_1032365824332445816390956_318234438793651778686071_n.jpg?oh=21fcf828fac0dd49e125b13028d57bfb&oe=56EB6A10"
]
While using the foreach of the facebook results I try to loop through the data field for all the contents yet I am returned with error:
Trying to get property of non-object (View: /var/www/web/elephantegin/htdocs/resources/views/socialApps/facebook.blade.php)
This is the code I used:
#foreach($results->data as $result)
<p>poops</p>
#endforeach
and alternatively this:
#foreach($results as $result)
#foreach($result->data as $data)
<p>help</p>
#endforeach
#endforeach
I've done this times before with no problem. In my controller use json_decode on the results before I push them to the view. How can i call this foreach properly?
If I get you right, then you have an array, but trying to get property. That is how it should be:
#foreach($results['data'] as $result)

Categories