In this Laravel tutorial, I will tell you about the 'Tinker' one of the awesome features in Laravel application which allow user to interact with entire Laravel applications from the command line.
You can put all eloquent queries on command line with the help of
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 the run the migration command to create 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 tinkerSeeding Database with Test Users
First, we will seed our database with 10 new user details by running following line of command :
You can count the total number of users in the database by running 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 = "email@example.com";
Update User Details
Run the query to update user details :
- $user = App\User::find(2);
- $user->name='Test User';
Run the following query to delete user from database :
- $user = App\User::find(1);