laravel database query code example
Example 1: laravel not in query
DB::table(..)->select(..)->whereNotIn('book_price', [100,200])->get();
Example 2: laravel update from query
$affected = DB::table('users')
->where('id', 1)
->update(['votes' => 1]);
Example 3: laravel wher in
$users = DB::table('users')
->whereIn('id', [1, 2, 3])
->get();
Example 4: laravel sql query
use Illuminate\Support\Facades\DB;
$users = DB::select('select * from users');
foreach ($users as $user) {
echo $user->name;
}
Example 5: laravel db query
$users = DB::table('users')
->where('votes', '>', 100)
->orWhere(function($query) {
$query->where('name', 'Abigail')
->where('votes', '>', 50);
})
->get();
Example 6: TRANSACTON LARAVEL QUERY BUILDER
DB::beginTransaction();
try {
DB::insert(...);
DB::commit();
} catch (\Throwable $e) {
DB::rollback();
throw $e;
}