In Laravel 12, you can insert data into the database using Eloquent ORM or the Query Builder. Below are examples of how to perform an insert query in Laravel 12 using both methods.
1. Using Eloquent ORM
Eloquent ORM provides a simple and expressive way to insert records into the database. You can create a new model instance, set its attributes, and then save it to the database.
Example:
<?php use App\Models\User; // Create a new user instance $user = new User(); // Set the attributes $user->name = 'John Doe'; $user->email = 'john.doe@example.com'; $user->password = bcrypt('password123'); // Save the user to the database $user->save(); ?>
Alternatively, you can use the `create()` method for mass assignment (make sure the `$fillable` or `$guarded` properties are set in your model):
<?php use App\Models\User; // Insert a new user using mass assignment $user = User::create([ 'name' => 'Jane Doe', 'email' => 'jane.doe@example.com', 'password' => bcrypt('password123'), ]); ?>
Model Setup:
Ensure your model has the `$fillable` or `$guarded` property to allow mass assignment.
<?php class User extends Model { protected $fillable = [ 'name', 'email', 'password', ]; } ?>
2. Using Query Builder
The Query Builder provides a more direct way to interact with the database. You can use the `insert()` method to insert one or multiple records.
Example: Insert a Single Record
<?php use Illuminate\Support\Facades\DB; // Insert a single record DB::table('users')->insert([ 'name' => 'John Doe', 'email' => 'john.doe@example.com', 'password' => bcrypt('password123'), 'created_at' => now(), 'updated_at' => now(), ]); ?>
Example: Insert Multiple Records
<?php use Illuminate\Support\Facades\DB; // Insert multiple records DB::table('users')->insert([ [ 'name' => 'John Doe', 'email' => 'john.doe@example.com', 'password' => bcrypt('password123'), 'created_at' => now(), 'updated_at' => now(), ], [ 'name' => 'Jane Doe', 'email' => 'jane.doe@example.com', 'password' => bcrypt('password123'), 'created_at' => now(), 'updated_at' => now(), ], ]); ?>
3. Insert and Get the Inserted ID
If you want to insert a record and retrieve the auto-incrementing ID of the inserted record, you can use the `insertGetId()` method.
Example:
<?php use Illuminate\Support\Facades\DB; // Insert a record and get the ID $userId = DB::table('users')->insertGetId([ 'name' => 'John Doe', 'email' => 'john.doe@example.com', 'password' => bcrypt('password123'), 'created_at' => now(), 'updated_at' => now(), ]); echo "Inserted User ID: " . $userId; ?>
4. Insert with Timestamps
If you want to automatically set the `created_at` and `updated_at` timestamps, you can use Eloquent ORM or manually add them in the Query Builder.
Using Eloquent ORM:
Eloquent automatically handles timestamps if `public $timestamps = true;` is set in your model.
<?php use App\Models\User; $user = User::create([ 'name' => 'John Doe', 'email' => 'john.doe@example.com', 'password' => bcrypt('password123'), ]); ?>
Using Query Builder:
You need to manually add the timestamps.
<?php use Illuminate\Support\Facades\DB; DB::table('users')->insert([ 'name' => 'John Doe', 'email' => 'john.doe@example.com', 'password' => bcrypt('password123'), 'created_at' => now(), 'updated_at' => now(), ]); ?>
5. Insert Ignoring Duplicates
If you want to insert a record only if it doesn’t already exist, you can use the `insertOrIgnore()` method.
Example:
<?php use Illuminate\Support\Facades\DB; DB::table('users')->insertOrIgnore([ 'email' => 'john.doe@example.com', 'name' => 'John Doe', 'password' => bcrypt('password123'), 'created_at' => now(), 'updated_at' => now(), ]); ?>
Conclusion
In Laravel 12, you can insert data into the database using:
-> Eloquent ORM for a more object-oriented approach.
-> Query Builder for a more direct and flexible approach.
Both methods are powerful and can be used depending on your specific use case. Always ensure proper validation and handling of sensitive data (e.g., passwords) when inserting records.