Error while joining with Laravel's Query Builder

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 a member function count () on a non-object

What can I have done wrong?

    
asked by anonymous 07.10.2016 / 19:44

3 answers

4

Query Builder of Laravel 5.2 does not return a class collection but a array simple, looks at the informational text in English :

  

Like raw queries, the get method returns an array of results where each result is an instance of the PHP StdClass object. You can access each column's value by accessing the column as a property of the object:

Then:

change this line

@if($data->count() > 0)

for this

@if(count($data) > 0)

In the Laravel version 5.3 it returns a class collection >:

  The get method returns an Illuminate\Support\Collection containing the results where each result is an instance of the PHP StdClass object. You can access each column's value by accessing the column as a property of the object:

    
07.10.2016 / 19:51
1

You could do with using Eloquent, like this:

Controller

public function getStockDanger()
{
    $data = Product::get();

    return view(
        'product.index',
        [
            'data' => $data,
            'data_category' => $this->category->all(),
            'nav' => $this->nav
        ]
    );
}

In Model Product you should have a relationship method:

class Product extends Model{
    public function stock(){
        $this->hasMany('App\Stock', 'product.id', 'product_id')->where('stock', '<=', 0);
    }
}

From there on View ...

@if($data->stock->count() > 0)
    @foreach($data->stock as $product)

        # Aqui pode deitar o pêlo.

    @endforeach
@endif
    
07.10.2016 / 20:47
0

In versions prior to Laravel 5.3, no query result of Fluent returns Collection , but a array .

As stated by Virgil, use count($data) instead of $data->count() .

Just giving more details, when you use DB::table() , you're using Fluent . It will return array , unlike Eloquent , which returns Illuminate\Database\Eloquent\Collection .

If you want to avoid problems or even maintain standardization in your code, you can use count($data) for both Eloquent and Fluent , since Collection of Laravel implements the Countable (when you use it, you can set a behavior for when it is called count in the class instance).

Read more at:

07.10.2016 / 20:03