Use PHP Artisan Tinker to debug your Laravel

Source: Internet
Author: User
This article translated from: Tinker with the Data in Your Laravel Apps with Php Artisan Tinker

Today, we will demonstrate how to quickly debug data in a database by introducing a less-known feature in Laravel. By using Laravel artisan built-in PHP artisan Tinker, we can easily see the data in the database and perform various desired operations.

Laravel Artisan Tinker is a REPL (read-eval-print-loop), REPL refers to the interactive command line interface, which allows you to enter a piece of code to execute and print the results directly to the command line interface.

How to Access database data quickly and easily?

I think the best way to do this is to enter the familiar commands below and 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;

Using PHP artisan Tinker, in fact, we can easily do this. Tinker is Laravel's own REPL, built on the basis of Psysh. It helps us to communicate more easily with our applications without the need to constantly use DD () and Die (). That's the pain of Print_r () and DD () for debugging a piece of code, and I think most of us can relate to it.

Before we use Tinker, let's start by creating a test project and call it scotchtest for the moment. If you have Laravel installer installed on your computer, do it first:

Laravel New Scotchtest

The Laravel installer computer is not installed, you can create this project by composer

Composer Create-project Laravel/laravel scotchtest--prefer-dist

Initialize database: Running migrations

After we have created our test project (scotchtest), we also need to create a new test database and perform a database migration (migrations) to initialize the database. In this tutorial, we use the Laravel default migration to be sufficient. First configure the database connection information in the. env file, and then prepare to perform the migration, and the default migration will help us build a users table and a password_resets table.

PHP Artisan Migrate

When the migration is complete, we should be able to see information like this:

Populate our Database

In general, we can use Laravel's model factory to quickly populate our database, which can help me to insert pseudo-data into the database to facilitate our testing. Now let's start using Tinker.

PHP Artisan Tinker

This command will open a REPL window for us to use. We have already performed migration, and now we can use the model factory to populate the data directly in the REPL.

Factory (App\user::class, Ten)->create ();

At this point, a collection containing 10 new user records will be printed on your terminal. Now we can check to see if these records have really been created.

App\user::all ();

Using the Count method, you can also see how many users the user model has in the database.

App\user::count ();

After executing App\user::all () and App\user::count (), my output will look like this, and your output should be similar to mine, just different from the generated content.

Create a new user

With 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 wrote in Laravel. So create a new user's code:

$user = new App\user; $user->name = "Wruce Bayne"; $user->email = "iambatman@savegotham.com"; $user->save ();

Now enter $user and you can see

Delete a user

To delete a user with ID 1:

$user = App\user::find (1); $user->delete ();

Review the comment document for a class/method

With tinker, you can view a comment document for a class/method in REPL. However, the document content depends on whether the class/method has a document comment block (docblocks).

Doc 
 
  
   
   # Replace with 
  
   
    
    function name or class FQN
  
   
 
  

For example, consult the note document of DD

View Source

We can also print out the source code of a class/method directly in the REPL

Show 
 
  

For example, view the source code of DD

Summarize

Laravel Tinker is a tool that allows us to debug Laravel more easily, and with it, there is no need to turn on local Service (server) for a simple debug. Especially if you want to test a small piece of code, you do not need to insert a variety of var_dump and die, and then to delete them after debugging, you only need PHP artisan tinker enough.

Original address: https://yii.im/post/tinker-with-the-data-in-your-laravel-apps-with-php-artisan-tinker/

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.