[英]Convert Raw SQL Query to Laravel Eloquent
我的項目是一個內容管理系統,它的新版本將集成到 Laravel 框架中; 並且有許多巨大的 SQL 查詢需要轉換為雄辯的語法。
我發現以下工具可以將我已經編寫的查詢轉換為雄辯的語法,但它不起作用。
誰能指導我轉換查詢或幫助我找到可以完成這項工作的工具?
請在下面找到我的查詢示例(請注意,我的大多數查詢看起來都很相似):
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
謝謝你。
我只是為此制作了一個工具。 你可以在這里免費使用
我認為這是最好的工具:)閱讀並了解文檔,因為沒有一種工具能夠針對每種情況自動完成。
所以這是我對您上面的查詢的嘗試:
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();
我找到了這個在線工具。 它可以幫助尋找該工具的人更快地完成工作。 它可能不會一直正確。
請使用此在線工具將 SQL 查詢轉換為 Laravel Builder。
轉換后的查詢應該顯示在 Laravel Builder 中:
DB::select(`id`,`name`,`code`)
->selectSub(`SELECT price FROM productprice WHERE id = p.id`, `price`)
->selectSub(`SELECT image FROM productpictures WHERE id = p.id LIMIT 1`, `image`)
->from(`product as p`)
->where(`categoryid`, `=`, 1)
->get();
真的你的 SQL 應該使用這樣的連接:
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;
您可以在此處閱讀有關左連接的更多信息。
此外,它可能不建議像這樣匹配 ID,最好在 productpictures 和 productprices 上有一個 product_id,然后你可以在 products 表上有一個外鍵約束。
但可惜這不是問題。 要使其成為 Laravel eloquent,您應該使用關系,然后您可以簡單地執行以下操作:
$Product = Product::select('id', 'name', 'code')
->with('ProductPrice', 'ProductPictures')
->where('categoryid', 1)
->get();
你將能夠像這樣把它拉出來:
$Product->id;
$Product->name;
$Product->code;
$Product->ProductPrice->price;
$Product->ProductPicture->image;
你試試這個方法
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);
此代碼通過示例創建了相同的 SQL 字符串。 但我認為這不是最好的方法!
您應該在 where 子句中提及表別名或表名,否則會給出完整性違規約束。
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();
Update accesos set salida="2019-05-05 12:00:00" where salida = "0000-00-00 00:00:00"
用這個
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();
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
將 SQL 查詢轉換為 Laravel 查詢
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.