谁能帮我把下面的 SQL 查询转换成 Eloquent?
select *
from products, categories
where products.productID=categories.productID and categories.categoryID = 5;
这是我试过的:
$products = DB::table('products') ->join('categories', function($join) {
$join->on('products.productID', '=', 'categories.productID')
->where('categories.categoryID',$categoryID)
} ->get();
最佳答案
问题可能是,$categoryID
在闭包(又名匿名函数)内不可用。为了能够“从外部”使用变量,您必须添加 use
$products = DB::table('products')->join('categories', function($join) use ($categoryID){
$join->on('products.productID', '=', 'categories.productID')
->where('categories.categoryID', '=', $categoryID);
})->get();
关于sql - 将 SQL 查询转换为 Laravel 4 Eloquent,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27661778/