Converting SQL query to Lavavel 4 Eloquent
Can anyone help me convert the following SQL query to Eloquent?
select *
from products, categories
where products.productID=categories.productID and categories.categoryID = 5;
Here's what I've tried:
$products = DB::table('products') ->join('categories', function($join) {
$join->on('products.productID', '=', 'categories.productID')
->where('categories.categoryID',$categoryID)
} ->get();
+3
source to share
1 answer
Probably the problem is that it is $categoryID
not available in the closure (as an anonymous function). To be able to use the variable "outside" you must adduse
$products = DB::table('products')->join('categories', function($join) use ($categoryID){
$join->on('products.productID', '=', 'categories.productID')
->where('categories.categoryID', '=', $categoryID);
})->get();
0
source to share