Alpha Olomi
Posted on May 3, 2023
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();
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
In your Tinker REPL:
$api = new Json\Api();
$users = $api->users()->list();
var_dump($users);
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);
- 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();
- Eloquent Relations: You can use Tinker to retrieve related models using Eloquent's relation methods:
>>> $user = App\Models\User::find(1);
>>> $user->posts()->get();
- DB Facade Usage: You can use Tinker to execute raw SQL queries using the DB facade:
>>> DB::table('posts')->where('views', '>', 10)->get();
- See Full List of Query Builder
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');
- 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();
- See Full List of Facades: You can use Tinker to explore the full list of available facades:
>>> $cacheRepository = app()->make('Illuminate\Contracts\Cache\Repository');
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...
- 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('/');
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();
- Seeder: You can use Tinker to seed the database with fake data using Laravel's
Seeder
:
>>> Artisan::call('db:seed');
- 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 😃
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.
Posted on May 3, 2023
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.