Luke Evers

Creating Tips With Laravel 5

Posted on 6 April 2015

Laravel 5 defaults with a welcome page that displays an inpiring quote which inspired me to create an easy way to display tips (or hints) on the administrative panel that I've been working on. Laravel's inspiring quote is just a class that only has one static function called quote that displays a random quote from the collection of quotes it has.

// Display an inspiring quote
echo \Illuminate\Foundation\Inspiring::quote();  

The Tip Class

The way the administrative panel works is that there are multiple panels and each one is specialized. Each panel should have their own list of tips. Instead of writing duplicate code, each specialized Tips class extends our main Tip class.

There are only five things that the Tip class needs:

  1. A string containing the tip
  2. An array of tips
  3. A handler function
  4. A to string function
  5. A constructor

A String Containing The Tip

We're going to be storing the tip here for when we print it out using the __toString function that we'll be writing later.

/**
 * The chosen tip.
 *
 * @var string
 */
protected $tip;  

A Handler Function

Since I started off by mocking the Inspiring class I checked to see exactly how it's doing what it's doing, and it's just using the Illuminate\Support\Collection class.

return Collection::make($array)->random();  

So my handler function looks similar:

/**
 * Handle the tip.
 *
 * @return void
 */
public function handle()  
{
    $this->tip = Collection::make($this->tips)->random();
}

A To String Function

With adding a __toString function, instead of ->print(), ->display() or any other kind of display function, we can just create a new Tip and echo it out.

echo new App\Tips\{Tip};  

The {Tip} in that previous block is refering to a class that extends the Tip class. We can just echo the Tip class, but unless we add an array of $tips to the class then we'll get an error. So let's add that:

/**
 * An array of tips.
 *
 * @var array
 */
protected $tips = [  
    'Write your own class that extends Tip'
];

When writing a class that extends Tip, all we will have in there is an array of tips that overwrites the default Tip class tips.

In our toString function all we do is return our tip that we saved earlier.

/**
 * Print out a tip
 *
 * @return string
 */
public function __toString()  
{
    return $this->tip;
}

A Constructor

The last thing we need in our Tip class is just a constructor! Our constructor calls our handler function which sets everything up.

/**
 * Create a new Tip instance.
 *
 * @param  array $tips
 * @return void
 */
public function __construct($tips = null)  
{
    if (!is_null($tips))
    {
        $this->tips = $tips;
    }

    $this->handle();
}

The way we use this class is to just print it out as we make it.

echo new App\Tips\Tip;  

If you decide you want to pass an array to the Tip class instead of extending it or adding your tips to the Tip class you can do that too!

echo new App\Tips\Tip(['a', 'b', 'c']);  

The Full Tip Class

Extending The Tip Class

Extending the Tip class is extremely easy. All you have to do is add an array of $tips and echo the class out when you want to use it! Let's make an example class that is an extension on the Tip class called NewUser.

comments powered by Disqus