Mysql query like this :
SELECT
GROUP_CONCAT(DISTINCT
CONCAT(
'ifnull(SUM(case when location_code = ''',
location_code ,
''' then quantity end),0) AS `',
location_code , '`'
)
) INTO @sql
FROM
item_details;
SET @sql = CONCAT('SELECT item_number,SUM(quantity) as "total_quantity", ', @sql, '
FROM item_details
GROUP BY item_number');
PREPARE stmt FROM @sql;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;
I want to convert it to laravel eloquent, but i'm confused. Because there exist many statement. There exist PREPARE
, EXECUTE
, SET
, DEALLOCATE
etc. You can see query above
How can I convert it to laravel eloquent?
You should make your query as a Mysql Procedure and run it against your database, I did slight adjustment to your query below,
then call your procedure from Laravel controller using Query Builder Approach like this,
then convert your result set into Laravel collection, then you can make pagination easily like this,
then return to view like this
and in your blade view template, you can access the data in a foreach loop and render in table or list view, to display the pagination, you can do like this
However I advice you caching the data to avoid repeated calls to procedure and increase the performance, since this way of building pagination will slow down your application if you have millions of data, if you need implementing Ajax pagination, you may refer to this article
It's mostly raw queries:
Try this:
This appears to be a duplicate (How to create select dynamic fields from a table in mysql?).
You asked for and received assistance on the other thread, but are asking here for how to actually implement the answer they gave there. (And have been rather combative about asking, I might add.)
It's generally best to ask for clarifications on the code you got as an answer in the question where it was given.