How to Create a User using Tinker in Laravel

Tinker allows you to interact with your entire Laravel application on the command line, including the Eloquent ORM, jobs, events, and more. To enter the Tinker environment, run the tinker Artisan command.

php artisan tinker or php artisan ti 

By using the tinker command line, we can create a new user or insert new data into the database.

Now run php artisan ti

Psy Shell v0.10.6 (PHP 7.4.16 — cli) by Justin Hileman
>>> User::create(["name"=> "larainfo","email"=>"[email protected]","password"=>bcrypt("123456")]);
=> App\Models\User {#4290
   name: "larainfo",
   email: "[email protected]",
   updated_at: "2021-04-22 08:23:28",
   created_at: "2021-04-22 08:23:28",
   id: 1,
  }
>>> 

Or you can explore different approaches to storing new data. Let’s see.

php artisan ti

>>> $user = new App\Models\User;
=> App\Models\User {#4301}
>>> $user->name = "larainfo";
=> "larainfo"
>>> $user->email= "[email protected]";
=> "[email protected]"
>>> $user->password=bcrypt('123456');
=> "$2y$10$uSdO/eBCQPNK3eVjXlSh.ulBVamZOhc.Hu5bp8Xzzb.uWyS3MSwRC"
>>> $user->save();
=> true
saim ansari
saim ansari

I'm Saim Ansari, a full-stack developer with 4+ years of hands-on experience who thrives on building web applications that leave a lasting impression. When it comes to tech, I'm particularly adept at Laravel, React, Tailwind CSS, and the Tall Stack