Convert Raw SQL Query to Laravel Eloquent

╄→гoц情女王★ 提交于 2020-05-17 07:22:07

问题


My project is a content management system & a new version of it will be integrated in a Laravel framework; and there are lots of huge SQL queries which needs to be converted into eloquent syntax.

I found the following tool to convert my already written queries into eloquent syntax, but it does not work.

Could anyone guide me to convert my queries or help me find a tool that could do the job?

Please find below a sample of my queries (note that most of my queries look similar):

SELECT id, name, code,
       ( SELECT price FROM productprice WHERE id = p.id ) AS price,
       ( SELECT image FROM productpictures WHERE id = p.id LIMIT 1 ) 
AS image
FROM product p 
WHERE categoryid = 1

Thank you.


回答1:


I found this Online Tool. It can help someone who looking the tool to do the job faster. It may not correct all the time.




回答2:


This is the best tool in my opinion :) reading and knowing the documentation, because no tool will be able to do it automagically for every case.

So here is my attempt on your query above:

DB::table('product as p')
   ->select([
       'id',
       'name',
       'code',
       'pp.price',
       'ppic.image'
   ])
   ->join('productprice as pp', 'pp.id', '=', 'p.id')
   ->join('productpictures as ppic', 'ppic.id', '=', 'p.id')
   ->where('categoryid', 1)
   ->get();



回答3:


use this

DB::table('product')
    ->select('id','name','code','productprice.price','productpictures.image')
    ->join('productprice','productprice.id' = 'product.id')
    ->join('image','productpictures.id' = 'product.id')
    ->where('product.categoryid',1);
    ->get();



回答4:


Hy try this way

  Model::select(
    'id', 
    'name', 
    'code', 
     DB::raw('( SELECT price FROM productprice WHERE id = p.id ) AS price,'),
     DB::raw('( SELECT image FROM productpictures WHERE id = p.id LIMIT 1 ) AS image')
    )   ->where('categoryid', 1);

This code create a same SQL string by the example. But i think this not the best way!




回答5:


Really your SQL should be using joins like so :

SELECT 
p.id,
p.name,
p.code,
pprice.price,
ppictures.image
FROM product p
LEFT JOIN productprice pprice on p.id = pprice.id
LEFT JOIN productpictures ppictures on p.id = ppictures.id
WHERE p.categoryid = 1;

You can read more here about left joins.

Also its probably not advisory to match ID's like that, it would be better to have a product_id on the productpictures and productprices and then you can have a foreign key constraint on that to the products table.

But alas that is not the question. To make this into laravel eloquent you should use relationships and then you can simply do something like this:

$Product = Product::select('id', 'name', 'code')
->with('ProductPrice', 'ProductPictures')
->where('categoryid', 1)
->get();

and you will be able to pull that out like so:

$Product->id;
$Product->name;
$Product->code;
$Product->ProductPrice->price;
$Product->ProductPicture->image;



回答6:


You should mention table alias name or table name in where clause otherwise it will give integrity violation constraint.

 DB::table('product as p')
                ->join('productprice', 'product.id', '=', 'productprice.id')
                ->join('productpictures','currencies.id','=','product.id ')
                ->select('p.id' ,'p.name' , 'p.code' , 'productprice.price', 
                  'productpictures.image as image')
                ->where('p.categoryid',1)
                ->get();



回答7:


Update accesos set salida="2019-05-05 12:00:00" where salida = "0000-00-00 00:00:00" 



回答8:


SELECT *
FROM tbl_users as tu
WHERE
    tu.rank!='Admin' AND
    #where there is space (less than 2 children)
    (SELECT COUNT(*)
        FROM tbl_users
        WHERE under_of=tu.username) < 2
ORDER BY
    #distance from top of tree
    tu.id,
    #by free space
    (SELECT COUNT(*)
        FROM tbl_users
        WHERE under_of=tu.username),
    #leftmost
    tu.id

Convert SQL query to Laravel query



来源:https://stackoverflow.com/questions/53079656/convert-raw-sql-query-to-laravel-eloquent

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!