T
T
thisall2020-10-14 11:07:14
Laravel
thisall, 2020-10-14 11:07:14

How to generate from SQL query Laravel Builder?

I have a SQL query, how can I rewrite it in Laravel Query Builder?

Request:

SELECT cat.ID AS ID, cat.Platform AS Platform, cat.Create_Date AS Create_Date, cat.Name AS Name, cat.Status AS Status, cat.Image AS Image, count(*) AS item_count FROM items AS it INNER JOIN category AS cat ON it.Item LIKE CONCAT('%"category":"', cat.ID, '"%') WHERE cat.Platform = '5f771d465f4191.76733056' GROUP BY cat.ID

Answer the question

In order to leave comments, you need to log in

1 answer(s)
L
Loli E1ON, 2020-10-14
@thisall

DB::select(`cat.ID as ID`,`cat.Platform as Platform`,`cat.Create_Date as Create_Date`,`cat.Name as Name`,`cat.Status as Status`,`cat.Image as Image`)
    ->addSelect(DB::raw(`count(*) as item_count`))
    ->from(`items as it`)
    ->join(`category as cat`, function($join) {
      $join->on(`it.Item`, `LIKE`, `CONCAT('%"category":"', cat.ID, '"%')`);
      })
    ->where(`cat.Platform`, `=`, `5f771d465f4191.76733056`)
    ->groupBy(`cat.ID`)
    ->get();

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question