What i want to do is convert this query , i want to use pagination but when i use query like this pagination not working
DB::select('SELECT *, ( ( weight * 400 ) + Making_charge) AS Sum FROM products WHERE '.$cat.' ='. $id.' ORDER BY sum ASC');
when i try to use pagination i get this error
Call to a member function links() on array (View:
i tried type casting but that did not work either
can anyone provide me valid reason or solution without disliking it
thank you
Answers:
Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.
Method 1
The reason why you are getting Call to a member function links() on array
is because the QueryBuilder returns an array. Not a collection as Eloquent.
I changed your SQL query to use double quotes in order to use string interpolation, and the work sum
to product_sum
, since sum
is a reserved word by mysql.
"SELECT *, (( weight * 400 ) + making_charge) as product_sum FROM products WHERE {$cat} = {$id} ORDER BY product_sum ASC";
Your query, using eloquent, will look as somethin like this:
Product::query()
->where($cat, $id)
->select(['*', DB::raw('(weight * 400 + making_charge) as product_sum')])
->orderBy('product_sum')
->get();
This query returns a collection. You can also paginate using laravel pagination:
$items_per_page = 10;
Product::query()
->where($cat, $id)
->select(['*', DB::raw('(weight * 400 + making_charge) as product_sum')])
->orderBy('product_sum') // The default direction is ASC
->paginate($items_per_page);
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0