How to use Laravel Tinker
In this Laravel tutorial, I will explain to you about the ‘Tinker’, one of the awesome features in Laravel application that allows a user to interact with entire Laravel applications from the command line.
You can put all eloquent queries on the command line with the help of Tinker.
With the help of Laravel’s lesser-known features, You can quickly read data from the Database in Laravel application.
Laravel tinker is a repl (Read–Eval–Print Loop) powered by the PsySH package.
Before tinker, install the Laravel application and then run the migration command to create a table:
php artisan migrate
After running migration command, you will see the following output :
Now run artisan command to enter into tinker environment :
php artisan tinker
Seeding Database with Dummy Users
First, we will seed our database with 10 new users details by running the following line of command :
factory(App\User::class, 10)->create();
You can count the total number of users in the database by running the following command :
App\User::count();
Adding a New User
You can create a user from the repl. I have already told you that you can put your eloquent queries just like you write code in Laravel application :
$user = new App\User;
$user->name = "Ajay";
$user->email = "ajay.agrahari09@gmail.com";
$user->password=bcrypt('123456');
$user->save();
Update User Details
Run the query to update user details :
$user = App\User::find(2);
$user->name='Test User';
$user->save();
Delete User
Run the following query to delete user from database :
$user = App\User::find(1);
$user->delete();
“In case I missed something, do let me know in the comments and I’ll add it!”
Cheers!!
If you want to hire an expert to tell you all about Laravel development and help you make a decision about it, we are just a click away – Submit your query with us. We will make it quick and easy for you!