Oh Dear is the all-in-one monitoring tool for your entire website. We monitor uptime, SSL certificates, broken links, scheduled tasks and more. You'll get a notifications for us when something's wrong. All that paired with a developer friendly API and kick-ass documentation. O, and you'll also be able to create a public status page under a minute. Start monitoring using our free trial now.

Things I learned from reading Laravel: Up and running

Original – by Freek Van der Herten – 3 minute read

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}', 'NewsItemController@index');

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.

Stay up to date with all things Laravel, PHP, and JavaScript.

You can follow me on these platforms:

On all these platforms, regularly share programming tips, and what I myself have learned in ongoing projects.

Every month I send out a newsletter containing lots of interesting stuff for the modern PHP developer.

Expect quick tips & tricks, interesting tutorials, opinions and packages. Because I work with Laravel every day there is an emphasis on that framework.

Rest assured that I will only use your email address to send you the newsletter and will not use it for any other purposes.


What are your thoughts on "Things I learned from reading Laravel: Up and running"?

Comments powered by Laravel Comments
Want to join the conversation? Log in or create an account to post a comment.