- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How would you insert a new user into a MySQL table from within Laravel?
You can insert a new user in the following ways as shown in the examples below. To insert a new user from Laravel you can do it using artisan seeder. To do that first create a seeder using the command below −
php artisan make:seeder UserSeeder
Once the command is executed, you will get the UserSeeder.php file inside database/seeders. Add the code to add user records inside users table in the seeder file as shown below.
Example 1
<?php namespace Database\Seeders; use Illuminate\Database\Seeder; use Illuminate\Support\Facades\DB; use Illuminate\Support\Str; class UserSeeder extends Seeder { /** * Run the database seeds. * * @return void */ public function run(){ foreach(range(1,1000) as $index) { DB::table('users')->insert([ 'name'=>Str::random(10), 'email'=>Str::random(10).'@gmail.com', 'password'=>Str::random(10) ]); } } }
Now run the command to execute the seeder file −
php artisan db:seed --class=UserSeeder
Once the command is executed you should be able to see user's table with the data in it.
Example 2
Using DB facade insert() method −
<?php namespace App\Http\Controllers; use Illuminate\Http\Request; use Illuminate\Support\Facades\DB; class UserController extends Controller { public function index() { $user = DB::table('users')->insert([ 'name' => 'Vishal Khanna', 'email' => 'vishal@email.com', 'password' => 'vishal123', ]); if ($user) { echo "User inserted successfully!"; } else { echo "Error while inserting user details"; } } }
The DB facade insert() method is used to insert the record in users table.
Output
The output of the above code is −
User inserted successfully!
Example 3
Using create() method from Laravel eloquent model −
<?php namespace App\Http\Controllers; use Illuminate\Http\Request; use App\Models\User; class UserController extends Controller { public function index() { $user = User::create(['name'=>'Ashish Sehgal', 'email'=>'ashish@gmail.com', 'password'=>'ashish123']); if ($user) { echo "User inserted successfully!"; } else { echo "Error while inserting user details"; } } }
Output
The output of the above code is −
User inserted successfully!
Example 4
Using Laravel eloquent to insert into users table by creating user object and using save() method as shown below −
<?php namespace App\Http\Controllers; use Illuminate\Http\Request; use App\Models\User; class UserController extends Controller { public function index() { $user = new User(); $user->name = 'Kiya Singh'; $user->email ='kiya@gmail.com'; $user->password='kiya123'; $user->save(); if ($user) { echo "User inserted successfully!"; } else { echo "Error while inserting user details"; } } }
Output
The output of the above code is as follows −
User inserted successfully!
- Related Articles
- How can we insert a new row into a MySQL table?
- Insert JSON into a MySQL table?
- How can we use INSERT() function to insert a new string into the value of a column of MySQL table?
- How can we insert data into a MySQL table?
- MySQL INSERT INTO SELECT into a table with AUTO_INCREMENT
- How to insert new row into table at a certain index using jQuery?
- How to write MySQL procedure to insert data into a table?
- Auto insert values into a MySQL table in a range?
- MySQL query for INSERT INTO using values from another table?
- How can we set up a MySQL User account by using INSERT INTO statement?
- How to insert only a single column into a MySQL table with Java?
- MySQL statement to copy data from one table and insert into another table
- How can a user start new MySQL transaction?
- How to insert new string within a string subsequent to removing the characters from the original string by using MySQL function?
- How do I INSERT INTO from one MySQL table into another table and set the value of one column?
