Warning: Undefined variable $project_ID in /home/axon/viralbamboo.com/wp-content/themes/vb/single.php on line 18
Laravel Tinker Introduction - Viralbamboo

Laravel Tinker Introduction

By Viralbamboo

Laravel Tinker Introduction

Laravel Tinker seems like an awesome CLI for literally “tinkering” around with your project’s data quickly and efficiently, unfortunately, there is little data on the internet pertaining to getting up and going for newbies. This article reviews the tinker REPL (read-eval-print loop), also known as an interactive top-level or language shell. It’s based on PsySH.

Prequisites

I’ll assume for the basis of this article that you have created a couple object models within Laravel. Your models can be Post and associated Comments, my example showcases a User with a One-Many relationship with an Addresses model. For more information relating to Eloquent relationships and their set-up skip over to Laravels Eloquent documentation.

For my demonstration, I have created the default Laravel user & associated migrations. From version 5.0 upwards you can simply run the following command:

php artisan make:auth

This command will be used on fresh applications and will install a layout view, registration and login views, as well as routes for all authentication endpoints. I’ll skip over this for now, for more information relating please head over to Laravel’s authentication documentation. Please create several dummy users and a connected model through a relationship of your desire.

Environment

I personally use Homestead, an official, pre-packaged Vagrant box. Over the last few years, the process of setup has become a breeze and it’s a joy to use. Let’s get going!. If you do not have a laravel project installed you can simply run:

laravel new ProjectName

If you have laravel installer installed on your machine then you can still use composer to install your project if this is the preferred option.

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

Accessing Laravels Tinker REPL

php artisan tinker

This command opens a repl for interacting with your Laravel application. You’re in and ready to rock.

Tip: When messing about in tinker, terminal, iTerm or when you use a common command to clear the screen is cmd-k on Mac.

Quick access to your models existing data within your database

Running simple and easily rememberable commands allow you access to that data you need quickly within your own application. Before accessing data if you require testing data Laravel intuitively provides a model factories which conveniently generate large amounts of database records for testing. This action can be performed from within the Seeder file or via Tinker.

Example functions and use case scenarios

// creation of 10 dummy users
factory(App\User::class, 10)->create();

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

// find a specific user and see their attributes
App\User::where('username', 'dillon')->first();

Creating a new user

$user = new App\User;
$user->name = "Clark";
$user->email = "superman@marvel.com";
$user->save();

Further tinker examples:

/*
/ First method returns the first element in the collection that 
/ passes a given truth test
*/ 
>>> $user = App\User::first();
=> App\User {#706
     id: 1,
     email: "dillon@archive.viralbamboo.com",
     title: "mr",
     first_name: "Dillon",
     last_name: "Kavanagh",
     created_at: "2017-02-19 21:44:28",
     updated_at: "2017-02-19 21:51:14",
   }

// Retreiving relationship of a user and the relationship type 
>>> $user->address();
=> Illuminate\Database\Eloquent\Relations\HasMany {#678}

// Retrieving inherited data
>>> $user->address;
=> Illuminate\Database\Eloquent\Collection {#699
     all: [
       App\Address {#702
         id: 1,
         user_id: 1,
         street_address: "68 Dame Street",
         suburb: "Inner City",
         town: "Dublin",
         postcode: "493857",
         country: "Ireland",
         created_at: "2017-02-19 21:44:28",
         updated_at: "2017-02-19 21:44:28",
       },
     ],
   }

Tinker Conclusion

Laravel’s Tinker is a dynamic REPL that allows us to interact without applications data with simplicity. There is no need to code small features when you can quickly run a few command lines from tinker.

info@viralbamboo.com
Viralbamboo was shortlisted for 3 eirSpiders awards from 2016-2017

2016-2017 Shortlisted × 3

🍪 We use cookies.

Know more Agree