Camron Cade

Artisan web development, mostly using Laravel.

9 posts under

Laravel Eloquent's ->firstOr() method

I stumbled on another hidden gem today. I've know that Laravel has both ->firstOrNew() and firstOrCreate() queries. But for more complicated scenarios, you can run firstOr() which accepts a callback as the first argument that is run if the query does not turn up any results. This is really useful if you...

Read More

Blade shortcut for echoing variables that may not be set

Laravel is one of those frameworks that has hidden tricks that are a delight to stumble across. This is one of them. In your blade templates, you've probably done something like this: {{ $day->common_sense }} This is all fine and dandy, but what if the day doesn't exist? What if the day doesn't...

Read More

DB instance without using facades in Laravel's Lumen

The point of Lumen is that it is light and fast, and so by default helpful tools like Eloquent and Facades are disabled. I like to try and keep it light when I am using Lumen, so I opt to not turn them on. If you, like me, are used to using the facade...

Read More

.active classes in menus with Blade templates

In many of my projects, I have a layout blade file that each page extends. As part of the layout, there is a menu bar that has has a different style for a link if you are currently on that page. If you are using bootstrap or foundation, if you apply the css class...

Read More

Cleanly handle the routing for static pages in Laravel/Lumen

I'm working on a small project using Laravel's Lumen that requires a few basic static pages. I was looking for a quicker and more elegant way to define each route and felt like I was repeating myself with every $app->get() function I was writing. I extracted the pages into an array and...

Read More

Limit the number of messages sent on one SMTP connection - Laravel 5

Many SMTP servers have limits on the number of messages that can be sent during any one SMTP connection. If you are sending mail using the built in Mail class with Laravel, any messages sent within a request cycle are sent using one SMTP connection by default. There is actually a quick and easy...

Read More