DEV Community

Alpha Olomi
Alpha Olomi

Posted on

Tinker like a 10x: Mastering Artisan Tinker REPL for Laravel Part 2

Introduction

Welcome back to our exploration of Tinker REPL in Laravel! In part one of this series, we explored the basics of using Tinker REPL in Laravel. In this article, we will delve into the practical applications of Tinker for various use cases. Tinker is a powerful tool that can execute PHP scripts, it allows developers to quickly test code snippets and experiment including Laravel-centric code like facades, helpers, and more. So let's take a look at how we can leverage Tinker to improve our workflow and productivity.

Read the first part of this series here: Tinker like a 10x: Mastering Artisan Tinker REPL for Laravel Part 1

Let's take a closer look at some of the most useful Tinker recipes.

Use Cases by Environment

Tinker can be used in a variety of environments, including development, production, and any environment with an accessible shell. Here are some examples of how Tinker can be useful in each environment:

  • In Development:
    • Using a plethora of recipes: Tinker allows developers to quickly test code snippets and experiment with Laravel-centric code, including facades, helpers, and more.
  • In Production:
    • For Debugging: Tinker can be used in production to quickly diagnose and fix issues, without the need for a full-blown development environment.
  • In Any Environment with an Accessible Shell:
    • For Debugging: Tinker can be used to quickly diagnose and fix issues, regardless of the environment.

Recipes

Tinker can be used to execute PHP in a variety of recipes, from simple database access to testing code with fakerble facades. Here are some examples of the most common Tinker recipes:

Basic PHP Execution

Tinker can be used to execute PHP code in a variety of ways, including:

Using Bare PHP Functions: You can use Tinker to execute bare PHP functions:

>>> phpinfo();
Enter fullscreen mode Exit fullscreen mode

Tinker can also be used to execute PHP code in a variety of ways. Here are some examples of the most common Tinker recipes:

Using an example of installing a PHP package and executing the class:

composer require alphaolomi/json-placeholder
Enter fullscreen mode Exit fullscreen mode

In your Tinker REPL:

$api = new Json\Api();

$users = $api->users()->list();

var_dump($users);
Enter fullscreen mode Exit fullscreen mode

Eloquent Access / Database Access

Tinker provides a simple and intuitive way to access Eloquent models and the database. Here are some examples of what you can do with Tinker:

  • Eloquent Retrieval: You can use Tinker to retrieve Eloquent models from the database:
>>> $user = App\Models\User::find(1);
Enter fullscreen mode Exit fullscreen mode
  • Eloquent Insertion: You can use Tinker to insert new records into the database:
>>> $user = new App\Models\User;
>>> $user->name = 'John Doe';
>>> $user->email = 'john.doe@example.com';
>>> $user->password = bcrypt('password');
>>> $user->save();
Enter fullscreen mode Exit fullscreen mode
  • Eloquent Relations: You can use Tinker to retrieve related models using Eloquent's relation methods:
>>> $user = App\Models\User::find(1);
>>> $user->posts()->get();
Enter fullscreen mode Exit fullscreen mode
  • DB Facade Usage: You can use Tinker to execute raw SQL queries using the DB facade:
>>> DB::table('posts')->where('views', '>', 10)->get();
Enter fullscreen mode Exit fullscreen mode

Facades Like Config, App

Tinker can also be used to access Laravel's built-in facades, such as config and app. Here are some examples of what you can do with Tinker:

  • Config: You can use Tinker to access the configuration values from your config files:
>>> config('app.name');
Enter fullscreen mode Exit fullscreen mode
  • Logging: You can use Tinker to log messages to Laravel's log files:
>>> Log::info('This is an info message');
// You can view the log file at storage/logs/laravel.log

// Assuming you have a SendMail job with log messages
>>> (\App\Jobs\SendEmail::dispatch($user))->dispatch();
Enter fullscreen mode Exit fullscreen mode
  • See Full List of Facades: You can use Tinker to explore the full list of available facades:
>>> $cacheRepository =  app()->make('Illuminate\Contracts\Cache\Repository');
Enter fullscreen mode Exit fullscreen mode

File System/ Storage Access

Tinker can be used to interact with Laravel's storage functionality, such as the storage and File facades. Here are some examples of what you can do with Tinker:

  • Using Bare PHP Function: You can use Tinker to execute bare PHP functions for interacting with files and directories:
// Create a file
>>> file_put_contents(storage_path('file.txt'), 'Hello World!');
>>> file_get_contents(storage_path('file.txt'));

// Create a directory
>>> mkdir(storage_path('test'));

// Check if file exists
>>> file_exists(storage_path('example.txt'));

// And more...
Enter fullscreen mode Exit fullscreen mode
  • Using Laravel Code: You can also use Tinker to interact with Laravel's Storage facade. Debugging stores such as remote storages like S3, has never been easier but with Tinker you can do it like a pro. Here are some examples of what you can do with Tinker:
// Local Storage
>>> Storage::disk('local')->put('file.txt', 'Hello World!');
>>> Storage::disk('local')->get('file.txt');

// S3 Storage
// Assuming you have configured S3 in your filesystems.php config file

// Create a file
>>> Storage::disk('s3')->put('file.txt', 'Hello World!');

// List all files in the root directory
>>> Storage::disk('s3')->files('/');
Enter fullscreen mode Exit fullscreen mode

Testing Code with Fakerble Facades

Tinker can also be used to test code with fakerble facades, such as Factory and Seeder. Here are some examples of what you can do with Tinker:

  • Factory: You can use Tinker to generate fake data with Laravel's Factory:
>>> $user = App\Models\User::factory()->create();
Enter fullscreen mode Exit fullscreen mode
  • Seeder: You can use Tinker to seed the database with fake data using Laravel's Seeder:
>>> Artisan::call('db:seed');
Enter fullscreen mode Exit fullscreen mode
  • Mocking: You can use Tinker to mock classes and methods using Laravel's Mockery:

Mockery is a powerful tool for mocking classes and methods. It's included in Laravel by default, and can be used to mock any class or method. Here are some examples of what you can do with Tinker:

>>> $user = App\Models\User::factory()->create();

>>> $userMockock = Mockery::mock('App\Models\User');

>>> $userMockock->shouldReceive('find')->once()->andReturn($user);

>>> $userMockock->find(1);
// Further explorations for you to explore ๐Ÿ˜ƒ

Enter fullscreen mode Exit fullscreen mode

And More

Tinker is a powerful tool with a lot of potential use cases, and this list is by no means exhaustive. You can use Tinker for anything from testing code snippets to exploring new packages and libraries.

Read the first part of this series here: Tinker like a 10x: Mastering Artisan Tinker REPL for Laravel Part 1

Conclusion

Tinker is a versatile and powerful tool that can help developers save time and increase productivity. Whether you're testing code snippets, exploring new packages and libraries, or debugging issues in production, Tinker is an essential tool in any Laravel developer's toolbox. I hope this guide has been helpful in exploring some of the most useful Tinker recipes, and that you feel confident in using Tinker to streamline your development process.

Top comments (1)

Collapse
 
alnahian2003 profile image
Al Nahian

Thanks for writing the part 2