Tutorial

Tinker with the Data in Your Laravel Apps with PHP Artisan Tinker

Draft updated on Invalid Date
author

Samuel Oloruntoba

Tinker with the Data in Your Laravel Apps with PHP Artisan Tinker

This tutorial is out of date and no longer maintained.

Introduction

Today we’ll talk about how to use one of Laravel’s lesser-known features to quickly read data from our Laravel applications. We can use Laravel artisan’s built-in php artisan tinker to mess around with your application and things in the database.

Laravel artisan’s tinker is a REPL (read-eval-print loop). A REPL is an interactive language shell. It takes in a single user input, evaluates it, and returns the result to the user.

A quick and easy way to see the data in your database.

Wouldn’t it be nice to see the immediate output of commands like:

// see the count of all users
App\User::count();

// find a specific user and see their attributes
App\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 the above pretty quickly. Tinker is Laravel’s own REPL, based on PsySH. It allows us to interact with our applications and stop dd()ing and die()ing all the time. You may be familiar with littering your code with print_r()s and dd()s to ascertain values at points during computation.

Setting Up the Project

Before we tinker with our application, let us create a demo project. Let’s call it ScotchTest. If you have the laravel installer installed on your computer, run this command.

  1. laravel new ScotchTest

For those without the Laravel installer on their computer, you can still use composer to create a new Laravel project.

  1. composer create-project laravel/laravel ScotchTest --prefer-dist

Database Setup: Running Migrations

After installing our demo Laravel project, we need to create a database and set up migrations. For this article, we will be using the default Laravel migrations. So we configure our .env file to point to the database you created for this test. The default migrations include creating a users table and a password_resets table.

From the root of the project, run

  1. php artisan migrate

After migrating our database, we should see something similar to

Laravel Artisan Tinker Initial Migration

Seeding our Database

By default, Laravel provides a model factory that we can use to seed our database. Now let’s begin to tinker with our application.

From the root of the Laravel project, run the following command.

  1. php artisan tinker

This command opens a repl for interacting with your Laravel application. First, let’s migrate our database. While in the repl, we can run our model factory and seed our database.

factory(App\User::class, 10)->create();

A collection of ten new users should show up on your terminal. We can then check the database to see if the users were actually created.

App\User::all();

To get the total number of users in our database, we can just call count on the User model.

App\User::count();

After running App\User::all() and App\User::count(), mine looks like this. You should get something similar to mine only difference being the data generated.

Laravel Artisan Tinker Seed Database

Creating a New User

From the repl, we can create a new user. You should note that we interact with this repl just like you would write code in your Laravel application. So to create a new user, we would do

$user = new App\User;
$user->name = "Sammy Shark";
$user->email = "sammy@example.com";
$user->save();

Now we can type $user to the repl and get something like this.

Laravel Artisan Tinker Create a New User

Deleting a User

To delete a user, we can just do

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

Reading a Function/Class Documentation

With tinker, you can check out a class or function documentation right from the repl. But it depends on the class or function having DocBlocks.

  1. doc <functionName> # replace <functionName> with function name or class FQN

Calling doc on dd gives us this.

Laravel Artisan Tinker Read a Function

Checking Source

We can also check out a function or class source code while in the repl using

  1. show <functionName>

For example, calling show on dd gives us this.

Laravel Artisan Tinker Check a Source

Conclusion

Laravel Tinker is a tool that can help us easily interact with our application without having to spin up a local server. Think of a simple feature you want to test in a couple of lines you’d delete from your project, use tinker instead.

Thanks for learning with the DigitalOcean Community. Check out our offerings for compute, storage, networking, and managed databases.

Learn more about our products

About the authors
Default avatar
Samuel Oloruntoba

author

Still looking for an answer?

Ask a questionSearch for more help

Was this helpful?
 
Leave a comment


This textbox defaults to using Markdown to format your answer.

You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

Try DigitalOcean for free

Click below to sign up and get $200 of credit to try our products over 60 days!

Sign up

Join the Tech Talk
Success! Thank you! Please check your email for further details.

Please complete your information!

Become a contributor for community

Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.

DigitalOcean Documentation

Full documentation for every DigitalOcean product.

Resources for startups and SMBs

The Wave has everything you need to know about building a business, from raising funding to marketing your product.

Get our newsletter

Stay up to date by signing up for DigitalOcean’s Infrastructure as a Newsletter.

New accounts only. By submitting your email you agree to our Privacy Policy

The developer cloud

Scale up as you grow — whether you're running one virtual machine or ten thousand.

Get started for free

Sign up and get $200 in credit for your first 60 days with DigitalOcean.*

*This promotional offer applies to new accounts only.