Lestah
330
17
Laravel

How did it end as an array?

Posted 4 months ago by Lestah

here's my controller


public function addAttributes(Request $request, $id=null)
    {
        $productDetails = Product::with('attributes')->where(['id' => $id])->first();
        $productDetails = json_decode(json_encode($productDetails));
        echo "<pre>"; print_r($productDetails); die;
    }

it gives me this


stdClass Object
(
    [id] => 3
    [category_id] => 2
    [product_name] => valenciaga
    [product_code] => 4
    [product_color] => black
    [description] => quality shoes running shoes
    [price] => 200
    [image] => 87910.jpeg
    [created_at] => 2019-02-20 11:51:18
    [updated_at] => 2019-02-20 11:52:28
    [attributes] => Array
        (
            [0] => stdClass Object
                (
                    [id] => 1
                    [product_id] => 3
                    [sku] => rt-1
                    [size] => small
                    [price] => 200
                    [stock] => 5
                    [created_at] => 2019-02-20 16:16:51
                    [updated_at] => 2019-02-20 16:16:51
                )

            [1] => stdClass Object
                (
                    [id] => 2
                    [product_id] => 3
                    [sku] => rt-2
                    [size] => medium
                    [price] => 300
                    [stock] => 10
                    [created_at] => 2019-02-20 16:16:51
                    [updated_at] => 2019-02-20 16:16:51
                )

now here my view page


@foreach($productDetails['attributes'] as $attribute)
                <tr class="gradeX">
                  <td>{{ $attribute->id }}</td>
                  <td>{{ $attribute->sku }}</td>
                  <td>{{ $attribute->size }}</td>
                  <td>{{ $attribute->price }}</td>
                  <td>{{ $attribute->stock }}</td>
                  <a rel="{{ $attribute->id }}" rel1="delete-product" href="javascript:"  class="btn btn-danger btn-mini deleteRecord">Delete</a>
                </tr>
                @endforeach


now i got an error like this

2/2) ErrorException Trying to get property of non-object

should i use


<td>{{ $attribute['id'] }}</td>


to view my data?

Please sign in or create an account to participate in this conversation.

Reply to

Use Markdown with GitHub-flavored code blocks.