How to Use Laravel Tinker

Do you want to know how to use Laravel Tinker? Using Tinker you can interact with your Laravel application through the command line. Laravel Tinker is powered by the PsySH package. In this article, I show you how to use Laravel Tinker.

Laravel is one of the most popular frameworks which is built in PHP. If you like a clean and neat code then Laravel should be your choice for building a web application. It comes with fantastic built-in features like Artisan commands, Eloquent ORM, Broadcasting etc. Tinker is also one of the useful features included in the core of Laravel.

Tinker is a REPL(read-eval-print loop). REPL allows users to interact with the application through the command line. It’s commonly used for interaction with Eloquent ORM, jobs, events, and more.

Why Need of Using Tinker in Laravel?

There are several scenarios where a user can use a Tinker. For instance, you need to add records in your database and you don’t have access to it. In such a case, you can interact with the database through Tinker. Even you can update, delete records in the database.

Tinker allows you to write PHP code in the command line.

How to Use Laravel Tinker

When you install Laravel, you got Tinker by default. The users don’t need to install Tinker separately. However, you may install it manually if needed using the command:

composer require laravel/tinker

To demonstrate the use of Tinker, let’s assume you have a table ‘products’ in the database. This table contains the columns name, description, etc.

Product Table

Now we are going to insert a row in the ‘products’ table using the Laravel Tinker.

Open the command prompt in the root directory of your Laravel project. First, create a model that interacts with the table ‘products’.

php artisan make:model Product

This command creates a model Product.php under the ‘app’ directory.

Next, to enter into the Tinker mode run the below command.

php artisan tinker

PHP Artisan Tinker

I will use the Eloquent ORM to insert a row in the ‘products’ table. So my code will be as follows in the command line.

$p = new App\Product;
$p->name = 'Test Product';
$p->description = 'Test Product Body';

PHP Artisan Tinker Insert Row

The above commands will insert a new row in the ‘products’ table.

The user can delete the record using the code below in a command line.

$p = App\Product::find(1); //here '1' is the product id

PHP Artisan Tinker Delete Row

Note: To come out of the Tinker, type ‘Exit’ and press Enter.

You can also call controller functions through Tinker. Let’s say you have below methods in the ProductController.php file.


namespace App\Http\Controllers;

use Illuminate\Http\Request;

class ProductController extends Controller
    public function index()
        return 'index';

    public function show($id)
        return $id;

    public function details($name, $id)
        return 'name: '. $name .'---id: '. $id;

Now, to call these methods write the below code in Tinker.

$controller = app()->make('App\Http\Controllers\ProductController');
app()->call([$controller, 'index'], []);
app()->call([$controller, 'show'], ['id' => 123']);
app()->call([$controller, 'details'], ['name' => 'John Doe', 'id' => '10']);

I hope you understand the basics of using a Tinker in Laravel. I would like to hear your thoughts and suggestions in the comment section below.

Related Articles

If you liked this article, then please subscribe to our YouTube Channel for video tutorials.

3 thoughts on “How to Use Laravel Tinker

Leave a Reply

Your email address will not be published.