5
I’m doing the following Join:
public function getStockDanger()
{
$data = DB::table('product')
->join('stock', 'product.id', '=', 'stock.product_id')
->where('stock.stock', '<=', 0)
->get();
return view(
'product.index',
[
'data' => $data,
'data_category' => $this->category->all(),
'nav' => $this->nav
]
);
}
View:
@if($data->count() > 0)
<div class="row">
<div class="col-md-12">
<table class="table table-striped table-responsive">
<thead>
<tr>
<th class="id text-center">#</th>
<th class="text-center">Produto</th>
<th class="text-center">Quantidade</th>
<th class="text-center">Preço</th>
<th class="text-center">Ações</th>
</tr>
</thead>
<tbody>
@foreach($data as $product)
<tr>
<td class="id text-center">{{$product->id}}</td>
<td class="text-center"><a class="color-red"
href="/products/{{$product->id}}">{{$product->name}}</a></td>
<td class="text-center @if($product->stock->stock <= 0) credit_danger @else credit_ok @endif">{{$product->stock->stock}}</td>
<td class="text-center credit_ok">R$ {{$product->sale_price}}</td>
<td class="text-center">
<a title="Editar" style="margin-right: 5px; color: #3498db; font-size: 18px;"
href="/products/edit/{{$product->id}}"><i class="fa fa-pencil"
aria-hidden="true"></i></a>
<a title="Excluir" style="color: #e74c3c; font-size: 18px;"
href="/products/delete/{{$product->id}}"><i class="fa fa-trash-o"
aria-hidden="true"></i></a>
</td>
</tr>
@endforeach
</tbody>
</table>
@include('pagination.default')
</div>
</div>
@else
<div class="alert alert-empty">
<strong>Nenhum cadastro!</strong> Tabela vazia
</div>
@endif
</div>
But I’m getting the following error
Fatalerrorexception in ac3f490a3eb54420e99fe37a225e6f129d87f36d.php line 36:
Call to a Member Function Count() on a non-object
What could I have done wrong?
It’s hard to find without the view. post line 36 of the view
product.index
.– Wallace Maxters
Another thing: Always report the version of Laravel.
– Wallace Maxters
Error in View line 36?
– novic
I posted the view. Laravel 5.2
– Felipe Paetzold
The problem seems to be that it is not being recognized as an object, but as I do to come a collection of objects?
– Felipe Paetzold
@Virgilionovic your answer is not wrong. Larvel 5.2 still returns a
array
.– Wallace Maxters
@Wallacemaxters let me confirm, there was so much change that I remember not returning
class collection
I’ll be right back!– novic