Object only becomes available when I give dd()?

Asked

Viewed 59 times

1

I am setting an attribute of an array, it is accessible only when giving dd() I do so:

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

In my controller I do so:

$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." >["Dscsubcategory"]=> string(40) "Category for gluten-free breads" >["Flgpontua"]=> int(0) ["Qtdpontos"]=> 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) >["Nmcategoria"]=> string(5) "Bread1" ["Dsccategory"]=> 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) ["Cdcategory"]=> int(5) >["Nmsubcategory"]=> string(29) "Gluten-free and lactose-free cake" >["Dscsubcategory"]=> string(33) "Gluten-free and lactose-free products" >["Flgpontua"]=> int(1) ["Qtdpontos"]=> int(10) >["Maxpontosporsubcategoria"]=> 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) { ["Cdcategoria"]=> int(5) >["Nmcategoria"]=> string(4) "Bolo" ["Dsccategoria"]=> string(29) "Category >destined 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) { ["CdSubCategoria"]=> int(4) ["CdCategoria"]=> int(7) >["NmSubCategoria"]=> string(7) "Farinha" ["DscSubCategoria"]=> string(23) >"Farinha para fazer bolo" ["FlgPontua"]=> int(0) ["QtdPontos"]=> int(0) >["MaxPontosPorSubCategoria"]=> 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:

Trying to get Property of non-object (View: C: Users Computer Desktop SGLE-Bakery PROJECTS Resources views Subcategory listSubCategory.blade.php)

Because this is happening ?

  • $subcategorias = SubCategoria::with('categoria')->get(); after that line that is in the one controller var_dump($subcategorias); and stick to the question!

  • updated the question

  • excuse makes a var_dump($subcategorias->toArray());!!! so is the collection object gets bad to see the results.

  • ready already att

  • There are categories that are coming NULL, I made an answer!

1 answer

1


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

["categoria"]=> NULL

then, from what I can gather there may be SubCategorias without Categorias?

If yes in your code foreach

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

Browser other questions tagged

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