db insert laravel code example

Example 1: laravel insert

DB::table('users')->insert([
    'email' => '[email protected]',
    'votes' => 0
]);

Example 2: laravel not in query

DB::table(..)->select(..)->whereNotIn('book_price', [100,200])->get();

Example 3: How to request and display data from db in larave

$users = DB::table('users')->get();
foreach($users as $key => $value)
{
	//to get each columns value
	//$value->name
}

Example 4: laravel wher in

$users = DB::table('users')
                    ->whereIn('id', [1, 2, 3])
                    ->get();

Example 5: laravel db inserr

$id = DB::table('users')->insertGetId(
    ['email' => '[email protected]', 'votes' => 0]
);

Example 6: TRANSACTON LARAVEL QUERY BUILDER

DB::beginTransaction();

try {
    DB::insert(...);    
    DB::commit();
} catch (\Throwable $e) {
    DB::rollback();
    throw $e;
}

Tags:

Php Example