Home > Software engineering >  Attempt to read property "id" on integer
Attempt to read property "id" on integer

Time:12-13

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

CodePudding user response:

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

CodePudding user response:

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