In this article, We will explain to you how to insert data into the database using Insert Query eloquent in laravel(laravel eloquent insert query example). 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.
DB::table('students')->insert([ 'first_name' => 'john', 'last_name' => 'doe', 'email' => 'john@gmail.com', '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.
Student::insert([ 'first_name' => 'john', 'last_name' => 'doe', 'email' => 'john@gmail.com', 'mobile' => '7878787', 'address' => 'USA', ]);
Please follow and like us: