Is this object only available when I give dd ()?

1

I'm setting an attribute of an array, it's only accessible when I give dd() as follows:

dd($categoria->categoria->NmCategoria)

It works if I do it the natural way like this:

 @foreach($subcategorias as $categoria)
    <tr>
      <td>{{ $categoria->CdSubCategoria }}</td>
      <td>{{ $categoria->categoria->NmCategoria }}</td>
    </tr>
@endforeach

On my controller I do this:

$subcategorias =  SubCategoria::with('categoria')->get();

if(Request::wantsJson())
{
    return $subcategorias;
}
else
{
    return view('SubCategoria.listSubCategoria', compact('subcategorias'));
}

var_dump of $subcategorias :

  

array (3) {[0] = > array (11) {["CdSubCategory"] = > int (1) ["CdCategory"] = > > int (3) ["NmSubCategory"] = > string (17) "Gluten-free pie." > [& quot; & quot; & quot; & quot; string (40) "Category for gluten-free breads" > ["FlgPontua"] = > int (0) ["QtyPoints"] = > int (1)> ["MaxPontosPorSubCategory"] = > int (0) ["created_at"] = > string (19)> "2016-09-10 14:25:22" ["updated_at"] = > string (19) "2016-09-14 22:16:03" > ["deleted_at"] = > NULL ["category"] = > array (6) {["CdCategory"] = > int (3) > ["CategoryName"] = > string (5) "Bread1" ["Breadcrumb"] = > string (31) "Category > intended for breads." ["created_at"] = > string (19) "2016-09-09 00:12:36" > ["updated_at"] = > string (19) "2016-09-09 00:12:36" ["deleted_at"] = > NULL}} > [1] = > array (11) {["CdSubCategory"] = > int (3) ["CategoryCode"] = > int (5)> ["NmSubCategory"] = > string (29) "Gluten-free and lactose-free cake" > ["DscSubCategory"] = > string (33) "Gluten-free and lactose-free products" > ["FlgPontua"] = > int (1) ["QtD Points"] = > int (10)> ["MaxPontosPorSubCategory"] = > int (0) ["created_at"] = > string (19)> "2016-09-10 20:34:29" ["updated_at"] = > string (19) "2016-09-10 20:34:29" > ["deleted_at"] = > NULL ["category"] = > array (6) {["CdCategory"] = > int (5) > ["CategoryName"] = > string (4) "Cake" ["String"] = > string (29) "Category" intended for cakes "[" created_at "] = > string (19) "2016-09-10 20:32:45" > ["updated_at"] = > string (19) "2016-09-10 20:32:45" ["deleted_at"] = > NULL}} > [2] = > array (11) {["CdSubCategory"] = > int (4) ["CategoryCode"] = > int (7)> ["NmSubCategory"] = > string (7) "Flour" ["DscSubCategory"] = > string (23)> "Flour for baking" ["FlgPontua"] = > int (0) ["QtyPoints"] = > int (0)> ["MaxPontosPorSubCategory"] = > int (0) ["created_at"] = > string (19)> "2016-09-21 23:00:26" ["updated_at"] = > string (19) "2016-09-21 23:00:26" > ["deleted_at"] = > NULL ["category"] = > NULL}}

It does not work and gives the following error:

  C: \ Users \ Computer \ Desktop \ Projects \ SGLE-Bakery \ resources \ views \ SubCategory \ listSubCategory.blade.php)

Why does this happen?

    
asked by anonymous 19.10.2016 / 02:27

1 answer

1

At the end of the result of var_dump() has a category that is coming NULL

["categoria"]=> NULL

So, as far as I can see, there can be SubCategorias without Categorias ?

If yes in your code of foreach

@foreach($subcategorias as $categoria)
 <tr>
   <td>{{ $categoria->CdSubCategoria }}</td>
   <td>{{ is_null($categoria->categoria) === false ? $categoria->categoria->NmCategoria : "sem categoria" }}</td>
 </tr>
@endforeach
    
19.10.2016 / 02:51