laravel DB::INSERT code example

Example 1: laravel insert

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

Example 2: TRANSACTON LARAVEL QUERY BUILDER

DB::beginTransaction();

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

Example 3: laravel select count

$count = DB::table('category_issue')->count();

Example 4: laravel find query

// Retrieve a model by its primary key...
$flight = App\Models\Flight::find(1);

// Retrieve the first model matching the query constraints...
$flight = App\Models\Flight::where('active', 1)->first();

// Shorthand for retrieving the first model matching the query constraints...
$flight = App\Models\Flight::firstWhere('active', 1);

Tags:

Php Example