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 built in PHP. If you like 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, and much more. 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, etc.

Why Need to Use Tinker in Laravel?

There are several scenarios where you can use a Tinker. For instance, you need to interact with your database and perform some CRUD operations. In this case, Tinker provides a quick and better approach. In addition to it, 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, in some situations, you may install it using the command:

composer require laravel/tinker

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

Product Table

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

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

php artisan make:model Product

This command creates a Product model under the app/Models directory.

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

php artisan tinker
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\Models\Product;
$p->name = 'Test Product';
$p->description = 'Test Product Body';
$p->save();
artisan-tinker-insert-row

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

The user can delete the record using the Tinker with the code below.

$p = App\Models\Product::find(1); //here '1' is the product id
$p->delete();
artisan-tinker-delete-row

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

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

<?php
 
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 how to use 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. Required fields are marked *