Updated April 11, 2023
Introduction to Laravel Tinker
Laravel’s tinker is actually a repl (read-eval-print loop). It is a very good interactive language shell. As a process, it accepts inside it a user’s information or input provided and evaluates it in detail after which it returns the exact result to the specific user who provides the input. It is powered by the PsySH console. It allows a user to interact from the command line. It is mainly and usually used to make simple changes in the database. Tinker is accessed with the help of the command line php artisan tinker which is provided by Laravel to the users extensively for making minute changes in the database.
What exactly is Laravel Tinker?
The command line that is used by Laravel is called artisan. And in the artisan, there are a few powerful features that come handy for the users or developers to make alterations as found necessary for themselves. Php artisan tinker is an artisan command which speeds up the workflow without delaying much. It gives allowance for the user to completely interact with the application in many ways. Being a Repl(Read-Eval-Print Loop), it provides you with a prompt which is then used to give allowance to interact with the user’s application using the native syntax of the language used by the application. REPL is a particular type of interactive shell. It collects user information and processes and evaluates the given information and then returns the final results to the user. Being an interactive language shell, it allows you to very easily and conveniently read the database and insert and delete data as per the needs.
You may wish to read and manipulate the available models in your app. Tinker is best way to handle this situation as it provides the amenities to resolve your problem and make sure your work is not hampered. Post::all () is one such command which is used particularly to explore the content of the database. The benefit is that you will not need to create the test route. Direct database manipulation is possible. So often when you are writing the codes, you aren’t sure of the syntax of the particular library you are exploring. Tinker makes it easier and convenient to try and manage bits of codes which is much faster than the execution of a script wherein you will have to wait for an error to appear or occur. There is a high risk that it may not even display any error. The functionality of the Laravel collection method is very effective.
Debugging of workflow is done by many Laravel developers who use the following function: insert add() dump and die in the code. The refreshing of the page is then done followed by a repeat of the process. It gets the job done but is time-consuming and may require you to repeat the work again and again. When you set up an xDebug, the workload gets easier. In tinker, you can replace dd () with a special part of the code: eval(\Psy\sh()); Through this one gets a tinker prompt that drops the user straight away to the given application and also to the exact point where there will be execution taking place.
How does Laravel Tinker exactly Work?
Crud operations can be conducted with the help of Laravel Tinker. We need to first install it with the help of the composer. The command to use is:
Composer requires Laravel/ tinker
1. Database Setup
For instance, we can create a database with a name like mentorcode and configuration can be done in the .env file available.
The default migration file for the user’s table will need to be migrated. Php artisan migrates command can be used for this processing.
2. Seeding database
Tinker’s use is made to seed the database very commonly. Populating the database with a single command is therefore made possible. A command like a factory (App\User::Class, 8) ->create(); allows you to seed your database with 8 rows of data.
3. Accessing the Database
App/User::all(); is used to access the user’s data list.
4. Inserting the record manually
Using manual mode to insert the records is also possible. We will have to get the model as needed and store it in one of the variables. Then the assigning of the attribute on the variable needs to be done. Then after this is done we can conveniently call for the save method.
5. Updating the database
Updating of any of the information from the database is also possible. We need to get the record, assign a new attribute and save it to the database.
6. Deleting the record
With the help of tinker as you can insert the data, you can also delete the records as and when found necessary.
7. Reading the documentation
Tinker also helps you to read functions documentation. You will have to type doc before the name of the function.
8. Checking of the source
While we are using the repl function, we can easily find the class source code. We can also check out a class or even function documentation which is done with the help of tinker. It depends on the class or the function which has DocBlocks.
Other commands which are available in tinker
- Trace- It prints the full stack trace.
- Wtf- It shows a few lines of the most recent exception’s backtrace.
- History- It lists the command history. There are also handy options available for searching history and also for replaying the history.
- Whereami- It prints the current execution location of the user along with the file and the line number.
Conclusion
Laravel tinker is used by many developers to do minor changes to their existing database. It also gives you an allowance for editing, adding data and deleting as and when found necessary and you can freely do batch editing or even chosen editing at your convenience.
Recommended Articles
This is a guide to Laravel Tinker. Here we discuss How does Laravel Tinker exactly Works along with the other commands which are available in tinker. You may also have a look at the following articles to learn more –