$users = DB::table('users')
// Match users whose last_name column starts with “A”
->where('last_name', 'like','A%')
// Match users whose age is less than 50
->where('age','<' ,50)
// Retrieve the records as an array of objects
->get();
select * from `users` where `last_name` like ? and `age` < ?
$orders = DB::table('orders')
// Match orders that have been marked as processed
->where('processed', 1)
// Match orders that have price lower than or equal to 250
->orWhere('price','<=' ,250)
// Delete records that match either criterion
->delete();
delete from `orders` where `processed` = ? or `price` <= ?
$users = DB::table('users')
// Match users that have the value of “credit” column between 100 and 300
->whereBetween('credit', [100,300])
// Retrieve records as an array of objects
->get();
select * from `users` where `credit` between ? and ?
$products = DB::table('products')
// Sort the products by their price in ascending order
->orderBy('price', 'asc')
// Retrieve records as an array of objects
->get();
$products = DB::table('products')
// Get products whose name contains letters “so”
->where('name','like','%so%')
// Get products whose price is greater than 100
->where('price','>', 100)
// Sort products by their price in ascending order
->orderBy('price', 'asc')
// Retrieve products from the table as an array of objects
->get();
$products = DB::table('products')
// Group products by the “name” column
->groupBy('name')
// Retrieve products from the table as an array of objects
->get();
// Use table “users” as first table
$usersOrders = DB::table('users')
// Perform a Join with the “orders” table, checking for the presence of matching
// “user_id” column in “orders” table and “id” column of the “user” table.
->join('orders', 'users.id', '=', 'orders.user_id')
// Retrieve users from the table as an array of objects containing users and
// products that each user has purchased
->get();
select * from `users` inner join `orders` on `users`.`id` = `orders`.`user_id`
// Use table “users” as first table
$usersOrders = DB::table('users')
// Perform a Left Join with the “orders” table, checking for the presence of
// matching “user_id” column in “orders” table and “id” column of the “user” table.
->leftJoin('orders', 'users.id', '=', 'orders.user_id')
// Retrieve an array of objects containing records of “users” table that have
// a corresponding record in the “orders” table and also all records in “users”
// table that don’t have a match in the “orders” table
->get();
select * from `users` left join `orders` on `users`.`id` = `orders`.`user_id`