Use PhpArtisanTinker to debug Your Laravel.
Today, we will introduce an unfamiliar feature in Laravel to demonstrate how to debug data in the database quickly. By using Laravel artisan built-in php artisan tinker, we can easily view the data in the database and perform various desired operations.
Laravel artisan tinker is a REPL (read-eval-print-loop). REPL is an interactive command line interface that allows you to enter a piece of code for execution, and print the execution result directly to the command line interface.
How to easily and quickly View database data?
I think the best way is to enter the following familiar commands and then immediately see the results:
// see the count of all usersApp\User::count();// find a specific user and see their attributesApp\User::where('username', 'samuel')->first();// find the relationships of a user$user = App\User::with('posts')->first();$user->posts;
With php artisan tinker, we can do this easily. Tinker is the REPL provided by Laravel. it is built based on PsySH. It helps us communicate with our applications more easily without having to constantly use dd () and die (). I think most of us can feel the pain of print_r () and dd () in order to debug a piece of code.
Before using tinker, let's create a test project called ScotchTest for the moment. If laravel installer has been installed on your computer, run:
laravel new ScotchTest
If Laravel installer is not installed on a computer, you can use composer to create this project.
composer create-project laravel/laravel ScotchTest --prefer-dist
Initialize database: Running Migrations
After creating our Test Project (ScotchTest), we also need to create a new test database and execute database migration (migrations) to initialize the database. In this article, it is enough to directly use Laravel's default migration. First, configure the database connection information in the. env file, and then prepare for migration. the default migration will generate a users table and a password_resets table.
php artisan migrate
When the migration is completed, we can see information similar to this:
Fill our database
Normally, we can use Laravel's model factory to quickly fill our database. it can help me insert pseudo data into the database for testing. Now let's start using tinker.
php artisan tinker
This command opens a REPL window for us to use. We have already executed migration. now we can use the model factory in REPL to fill in the data directly.
factory(App\User::class, 10)->create();
At this time, a set containing 10 new user records will be printed on your terminal. Now we can check whether these records have been created.
App\User::all();
Using the count method, you can also view the total number of users in the database in the User model.
App\User::count();
After executing App \ User: all () and App \ User: count (), my output looks like this. your output should be similar to mine, only the generated content is different.
Create a new user
Through REPL, we can also create a new user. You should have noticed that the commands we use in REPL are the same as the code we write in laravel. So create a new user code:
$user = new App\User;$user->name = "Wruce Bayne";$user->email = "iambatman@savegotham.com";$user->save();
Enter $ user.
Delete a user
To delete a user with id 1:
$user = App\User::find(1);$user->delete();
View comments of a class/method
Tinker allows you to view comments of a class/method in REPL. However, the document content depends on whether this class/method has a document comment block (DocBlocks ).
doc
# replace
with function name or class FQN
For example, read the comment document of dd.
View source code
We can also print the source code of a class/method in REPL.
show
For example, view the dd source code.
Summary
Laravel Tinker is a tool that makes it easier for us to debug laravel. with it, there is no need to enable the local service (server) for simple debugging ). Especially when you want to test a small piece of code, you don't need to insert various var_dump and die, and delete them after debugging. you only need php artisan tinker.
Address: https://yii.im/post/tinker-with-the-data-in-your-laravel-apps-with-php-artisan-tinker/