laravel eloquent insert query example
In this article, We will explain to you how to insert data into the database using Insert Query eloquent in laravel. The laravel eloquent provides many types of eloquent methods. so we can easily insert data into the database using the Insert Query eloquent method.
Laravel Insert Query Without Use Model
In this step, we load the DB and pass the table name and data into the insert method. so we can directly pass data without a model. see following below example.
1 2 3 4 5 6 7 | DB::table('students')->insert([ 'first_name' => 'john', 'last_name' => 'doe', 'mobile' => '7878787', 'address' => 'USA', ]); |
Laravel Insert Query Using Model
In this step, first, we load the student model and after then we can pass data by the insert method using this model. so you can see following below example.
1 2 3 4 5 6 7 | Student::insert([ 'first_name' => 'john', 'last_name' => 'doe', 'mobile' => '7878787', 'address' => 'USA', ]); |
Please follow and like us: