Tuesday, February 20, 2018

Laravel eloquent not getting the results - development

Laravel eloquent not getting the results

I have a laravel project and I don’t see where is the bug here.

I have a products table with a “low_stock_warning” integer field and a “units_stock” integer field.

I want to get the products with a “units_stock” less than “low_stock_warning“, so I have

$stock_warning = Product::whereRaw('units_stock <= low_stock_warning')
    ->select('id')
    ->get();

The result mysql is:

select `id` from `products` where units_stock <= low_stock_warning

If I execute it in my database directly I get one product. Ok, but Eloquent tells me that $stock_warning is an empty array…

What I’m missing here ?

Thanks!

enter image description here

EDIT 1:

I’ve tried with:

$stock_warning = Product::whereColumn('units_stock', '<=', 'low_stock_warning')->pluck('id');

print_r($stock_warning);

And the result is:

IlluminateSupportCollection Object ( [items:protected] => Array ( ) )



from Laravel Questions and Answers https://laravelquestions.com/php/laravel-eloquent-not-getting-the-results/
via Lzo Media

No comments:

Post a Comment