Table of Contents
Example 4
Output
Home Backend Development PHP Tutorial How to insert new user into MySQL table in Laravel?

How to insert new user into MySQL table in Laravel?

Sep 04, 2023 pm 02:25 PM
mysql laravel insert

How to insert new user into MySQL table in Laravel?

You can insert a new user as shown in the following example. To insert new users from Laravel you can use artisan seeder. To do this, first create a seeder−

using the following command
php artisan make:seeder UserSeeder
Copy after login

Once the command execution is completed, you will get the UserSeeder.php file in the database/seeders directory. Add the following code to the seed file to add user records to the users table.

The Chinese translation of

Example 1

is:

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)
         ]);
      }
   }
}
Copy after login

Now run the command to execute the seeder file−

php artisan db:seed --class=UserSeeder
Copy after login

Once you execute the command, you should be able to see the data in the users table.

The Chinese translation of

Example 2

is:

Example 2

Using the insert() method of the DB facade -

<?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";
      }
   }
}
Copy after login

DB facade insert() method is used to insert records into the user table.

Output

The output of the above code is −

User inserted successfully!
Copy after login
Copy after login
Copy after login

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";
      }
   }
}
Copy after login

Output

The output of the above code is −

User inserted successfully!
Copy after login
Copy after login
Copy after login
The Chinese translation of

Example 4

is:

Example 4

Use Laravel eloquent to insert data into the user table by creating a user object and using the 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";
      }
   }
}
Copy after login

Output

The output of the above code is as follows −

User inserted successfully!
Copy after login
Copy after login
Copy after login

The above is the detailed content of How to insert new user into MySQL table in Laravel?. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot Article Tags

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to optimize MySQL query performance in PHP? How to optimize MySQL query performance in PHP? Jun 03, 2024 pm 08:11 PM

How to optimize MySQL query performance in PHP?

How to use MySQL backup and restore in PHP? How to use MySQL backup and restore in PHP? Jun 03, 2024 pm 12:19 PM

How to use MySQL backup and restore in PHP?

Laravel - Artisan Commands Laravel - Artisan Commands Aug 27, 2024 am 10:51 AM

Laravel - Artisan Commands

How to insert data into a MySQL table using PHP? How to insert data into a MySQL table using PHP? Jun 02, 2024 pm 02:26 PM

How to insert data into a MySQL table using PHP?

How to fix mysql_native_password not loaded errors on MySQL 8.4 How to fix mysql_native_password not loaded errors on MySQL 8.4 Dec 09, 2024 am 11:42 AM

How to fix mysql_native_password not loaded errors on MySQL 8.4

How to use MySQL stored procedures in PHP? How to use MySQL stored procedures in PHP? Jun 02, 2024 pm 02:13 PM

How to use MySQL stored procedures in PHP?

Comparison of the latest versions of Laravel and CodeIgniter Comparison of the latest versions of Laravel and CodeIgniter Jun 05, 2024 pm 05:29 PM

Comparison of the latest versions of Laravel and CodeIgniter

How do the data processing capabilities in Laravel and CodeIgniter compare? How do the data processing capabilities in Laravel and CodeIgniter compare? Jun 01, 2024 pm 01:34 PM

How do the data processing capabilities in Laravel and CodeIgniter compare?

See all articles