Trying to get Property of non-object error while trying to access the Blade view in the Laravel

Asked

Viewed 376 times

1

I am trying after a foreach to access the variable with the object array in the Slide but it displays the Trying to get Property of non-object error. I’ve tried with the "firt" but it only takes the first of house loop in the query, however it works, could help me?

    <?php 
         $artigo = 'short';

         $get_cat=  DB::table('products')
         ->where('category', '=', $artigo)
         ->select('id_product')
         ->get();
         
         ?>
  <div>
    <a href="#" data-open-in="picker" data-picker-close-text="OK" data-back-on-select="true" class="item-link smart-select" >
     <select  class="products_qto select-filter-size"  name="product" id="product-qto" >
        <option selected>TAM</option>
         -- @foreach ($get_cat as $key => $tam_prod)
         <?php
            $tam = DB::table('sizes')
              ->where('id_product', '=', $tam_prod->id_product)
              ->select('title')
              ->get();  
          ?>
        <option value="{{$tam->title}}">{{$tam->title}}</option>
   @endforeach   
    </select>                                                                                                         
   </a>
  </div>
    <a href="#" onclick="cancelSearchProducts()">LIMPAR</a>
</div>

1 answer

0


There are some points in your code that are not very good, and perhaps impacting for this problem of yours. I advise to take a look at Relations and make this information come from the controller.

But about the mistake: The error itself says that there is not one of the properties in this object that you are trying to iterate, and if with the first() is working means that the Collection that is being assigned in its $tam variable is bringing another array of objects, you need to iterate it also doing so a second loop:

    <?php 
         $artigo = 'short';

         $get_cat=  DB::table('products')
         ->where('category', '=', $artigo)
         ->select('id_product')
         ->get();
         
         ?>
  <div>
    <a href="#" data-open-in="picker" data-picker-close-text="OK" data-back-on-select="true" class="item-link smart-select" >
     <select  class="products_qto select-filter-size"  name="product" id="product-qto" >
        <option selected>TAM</option>
         @foreach ($get_cat as $key => $tam_prod)
         <?php
            $tam = DB::table('sizes')
              ->where('id_product', '=', $tam_prod->id_product)
              ->select('title')
              ->get();  
          ?>
        @foreach ($tam as $tam_item)
          <option value="{{$tam_item->title}}">{{$tam_item->title}}</option>
        @endforeach   
   @endforeach   
    </select>                                                                                                         
   </a>
  </div>
    <a href="#" onclick="cancelSearchProducts()">LIMPAR</a>
</div>

But remembering this way is not advised, you could create a relation of products and Sizes, it would be very easy and would not be so strange to have to make these 2 foreach and 2 querys just to bring the list of product size.

  • 1

    Oops, all right? I know the code is pretty messed up, but the system wasn’t developed for me, I’m just making some adjustments to a client. The dev that developed shook the project before making controllers and the money they are paying, for now, does not include this job or relate the table in the BD, just an msm, but thank you very much for the reply!

  • 1

    About the solution, with get o &tam this really with an array of arrays, which he builds every loop in the loop, worked right here Thank you very much!

  • You’re welcome.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.