svg image

Beginner's not-a-guide to Laravel - 4 things that will save you time

Paul

Paul

Developer

If you've shouted at Stack Overflow while looking for Laravel ‘how-to's, the 4 tips below are for you. Here at Global Initiative we're coding up our most recent project using the PHP framework Laravel. As someone who has done work with Drupal and also with a more bare-bones CMS framework in Django, a lot of the concepts and syntaxes in Laravel were familiar to me. I was up and running fairly quickly and coasting towards being an A1 expert Laravel dev... or so I thought. This bloglet isn't designed to be an introduction to Laravel, there's plenty of those already such as from the official docs here or here.

 

It's the little quirks and hidden functionality that caught me out and forced me to look at the dauntingly extensive documentation. So to save you, the beginner Laravel dev, some painful research time here are some handy tips that took a while to find. nl2br (and other string utility methods) Got a form with an address field? Contact form that uses a textarea and emails someone? To stop your address display or emails looking like one long string, you can use the nl2br PHP utility method in your blade template to http://laravel.io/forum/01-22-2015-keep-line-breaks.

 

Laravel also has a number of string formatting utilities for common operations (Sentence case, slugify, etc.). You may find it helpful to use some of these in a... Presenter Presenters allow an easy way to have models display differently in a variety of contexts, without having to have hundreds of methods on your object classes. For example, you may have an online store with a Product and you may want to have display change for whether the logged in user is a store owner or a customer. In this case you can use a Presenter to show prices in different ways. You can have individual convenience methods or even a secondary view, returning a chunk of renderable information in one go. It's easy to implement and use - register your class as having PresentableTrait and start writing your Presenter to go with it.

 

Stacks If you have a module which needs to include a JavaScript file in certain templates but not all, you can use the @stack directive to include the file in a way that won't overwrite other includes, meaning you can store them in a single location (useful for Grunt minification where the files need to be between sets of identifier tags). More about stacks here. Seeding Autopopulate data into your database for testing easily with a Seeder. You can make this class-based, you can create ModelFactories, trigger other seeders for cascading data etc. More info here. Laravel is really flexible and allows you to create powerful web apps and sites. The Artisan command line utility gives you a well-structured codebase with minimal fuss. If these tips save you an hour or so, then this bloglet will have been a rip roaring success.

Laravel Logo Party
21st December 2018
Dash line

More Technical

Who's the driver?
Daniel

Daniel

Developer
Store and share business documents securely
Yong

Yong

Developer
Playing with Database Queries
Jamie

Jamie

Developer