In this article, We’ll see how to use Laravel Tinker? Tinker allows you to interact with your entire Laravel application on the command line, including the Eloquent ORM, jobs, events, and more.
Tinker is the Laravel’s built-in feature, We don’t need to install them separately.
Let’s create a table i.e ‘subjects’ in the database using migration. This table contains the columns i.e id, name, author etc.
To create a migration, use the
make:migration artisan command:
php artisan make:migration create_subjects_table
The migration will be placed in your
Open the xxxx_xx_xx_xxxxxx_create_subjects_table.php in your text editor and add the below two line in create method on the
To run migration, run the below artisan command:
php artisan migrate
this migration creates a subjects table.
Now we’ll insert a row in the ‘subjects’ table using the Laravel Tinker.
Open the command prompt or terminal in your Laravel project. To use the tinker, we create a model which interacts with the table ‘subjects’.
Below command create a model i.e ‘Subject.php’ under the ‘app’ directory.
php artisan make:model Subject
Next, run the below command.
php artisan tinker
We use the Eloquent ORM to insert a row in our database’s table ‘subjects’.
$subject = new App\Subject; $subject->name = 'Laravel'; $subject->author = 'Taylor Otwell'; $subject->save();
Write the above code in command-line.
To update the record, we write the below code in a command-line.
$subject = App\Subject::find(1); // Retrieve a model by its primary key... $subject->name = 'CodeIgniter'; $subject->author = 'EllisLab'; $subject->save();
To delete the record, we write the below code in a command-line.
$subject = App\Subject::find(1); // Retrieve a model by its primary key... $subject->delete();
That’s it!. Please share your thoughts or suggestions in the comments below.