How to Create a User using tinker in laravel 9

How to Create a User using tinker in laravel 9. Laravel Tinker is a powerful REPL for the Laravel framework, powered by the PsySH package.

Also Read : Laravel 9 OrderBy ASC and DESC Tutorial

Installation

All Laravel applications include Tinker by default. However, you may install Tinker using Composer if you have previously removed it from your application:

composer require laravel/tinker

Usage

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

php artisan tinker

You can publish Tinker’s configuration file using the vendor:publish command:

php artisan vendor:publish --provider="Laravel\Tinker\TinkerServiceProvider"

Also Read : Laravel 9 Shopping Cart Tutorial with Ajax Example

Example – Create a User using tinker

Using tinker command line we can create new user or insert new data in database

Run,

php artisan tinker
Psy Shell v0.10.6 (PHP 7.4.16 — cli) by Justin Hileman
>>> User::create(["name"=> "larainfo","email"=>"laraveltuts@gmail.com","password"=>bcrypt("123456")]);
=> App\Models\User {#4290
   name: "laraveltuts",
   email: "laraveltuts@gmail.com",
   updated_at: "2022-05-28 08:23:28",
   created_at: "2022-05-28 08:23:28",
   id: 1,
  }
>>> 

or you can use different approaches to store new data let see. Run,

php artisan tinker
>>> $user = new App\Models\User;
=> App\Models\User {#4301}
>>> $user->name = "laraveltuts";
=> "laraveltuts"
>>> $user->email= "laraveltuts@gmail.com";
=> "laraveltuts@gmail.com"
>>> $user->password=bcrypt('123456');
=> "$2y$10$uSdO/eBCQPNK3eVjXlSh.ulBVamZOhc.Hu5bp8Xzzb.uWyS3MSwRC"
>>> $user->save();
=> true

Also Read : How to Generate and Read Sitemap XML File in Laravel 9 Tutorial

2 Comments

Leave a Reply