Every two weeks I send out a newsletter containing lots of interesting stuff for the modern PHP developer. You can expect quick tips, links to interesting tutorials, opinions and packages. Want to learn the cool stuff? Then sign up now!

Things I learned from reading Laravel: Up and running

Matt Stauffer is currently putting the final touches on his new book called Laravel: Up And Running. It aims to be a good guide for newcomers to the framework. But even if you’ve got some experience with Laravel, it should be worth your time to read it. Even Matt himself picked up a lot of cool stuff while writing it.

During my holiday I read an advance copy of the book containing all chapters expect the ones that touch on Laravel 5.3 specific functionality. I can say that it is very well written and it complements the official documentation well. Here are some of the things I’ve learned from reading this book.

Changing the route key for an Eloquent model

Consider this route:

Route::get('/newsItem/{newsItem}', [email protected]');

By default when using route model binding, Laravel will use the id of a model to fetch it. So a url like /newsItem/1 will work. Did you know the used route key can be changed by adding this code to the newsItem model?

public function getRouteKeyName() {
   return 'slug'; 

Given that the slugcolumn of one of your records contains my-slug this url will work: /newsItem/my-slug.

Sending a download response

Laravel makes it easy to respond with a download. You can even set the name the user will see in the browser.

Response::download($pathToFile, $nameOfDownload);

Using a default value in a Blade view

Whenever you use a variable in a Blade and you want to set a default value in case the variable is not set you can use or.

//inside a blade view
{{ $myVariable or "This variable was not set" }}

Sharing a variable with all Blade views

You can share a variable globally in all Blade views with share.

view()->share('currentUser', auth()->user());

You could put this code in a service provider.

Using closure based commands.

Instead of creating a separate class for an Artisan command you could define it using a closure inside the commands method of App/Console/Kernel.php

// App/Console/Kernel.php
protected function commands()
      'command:run {myArgument} {--myOption}', 
       function ($myArgument, $myOption) {
          // do something useful here...

Getting records in a random order

The query builder has a method inRandomOrder() to sort the retrieve models in a random order.

$randomUser = DB::table('users')

Getting a single value from a record

If you just need a single value from the first row you can use the value method.

$newestNewsItemTitle = DB::table('news_items')
   ->orderBy('created_at', 'desc')

Adding a uuid column

Inside migrations you can use the uuid method to add a column that’ll hold a uuid.

//inside a migration

For MySQL this will create a VARCHAR(36) column.

Eager load the number of related records

If you only need the count of the number of some related records you can use withCount.

$newsItems = NewsItem::withCount('comments')->get();

These are just a few of the bits I picked up from reading the book. I can highly recommend the book to anyone using the framework (That’s my honest opinion, I’m not being paid to say this :-)). I don’t know on which date the book will be released publicly, but I guess it’s rather soonish. Keep an eye on the official site of the book.

Freek Van der Herten is a partner and developer at Spatie, an Antwerp based company that specializes in creating web apps with Laravel. After hours he writes about modern PHP and Laravel on this blog. When not coding he’s probably rehearsing with his kraut rock band.
  • I’ve bought every Laravel book thats come out and was planning on giving this a skip but after reading the tips, a number of which I didnt know, I think I’ll purchase it.

  • Mygmarsuren Sedjav

    Wow very useful features.

  • Hi, thanks for the article.

    I’m interested in ‘getRouteKeyName’.

    Say I have a ‘carNo’ field and I’ll like to fecth /order/{carNo} instead of /order/{$id}

    Do I just need to place the following in my Order Model

    public function getRouteKeyName() {
    return ‘carNo’;

    Kinda confused here