Looking for more information on how to do PHP the right way? Check out PHP: The Right Way

Jonathan Reinink:
Dynamic relationships in Laravel using subqueries
Dec 05, 2018 @ 12:50:20

In a recent post to his site Jonathan Reinink has written up a guide to using dynamic (Eloquent) relationships in Laravel applications by making use of subquery functionality. In it, he shows how to make use of the selectSub method to select additional information in a single query versus having the overhead of custom, hard-coded relationships.

When building web apps that interact with a database, I always have two goals in mind: keep database queries to a minimum [and] keep memory usage to a minimum. These goals can have a drastic impact on the performance of your app.

Developers are typically pretty good at the first goal. We're aware of N+1 style problems, and use techniques like eager-loading to limit database queries. However, we're not always the best at the second goalkeeping memory usage down. In fact, we sometimes do more harm than good trying to reduce database queries at the expense of memory usage.

He starts off with the challenge he's trying to solve: gathering login information for users in a performant way. He includes the schema for the users and logins table and shows the code of how a normal relationship select might look to get login information for each user (creating an N+1 issue).

To help solve the issue, they try caching the last login information but realize they can do better - this is where subqueries come in. They provide an example of using the selectSub method to get the login information, mapping it to a macro for easier use and defining scopes. Finally, the tutorial shows how to use this method to select information via dynamic relationships. It also talks about lazy-loading issues and if the same thing could be accomplished with a "has one" relationship.

tagged: tutorial laravel eloquent dynamic relationship subselect database query

Link: https://reinink.ca/articles/dynamic-relationships-in-laravel-using-subqueries

Laravel News:
2018 Laravel Survey Results
Dec 04, 2018 @ 12:54:50

On the Laravel News site, they've posted the announcement of the release of the results from their 2018 edition of the Laravel Survey.

We partner with LaraJobs to run an annual Laravel survey to see what types of projects people are taking on with Laravel as well as get some feedback on what the Laravel community could be doing better. The results ended up with over 1,500 submissions, and some interesting insights.

You can see the complete results on this 2018 Laravel Survey Results page and thanks for everyone who took the time to fill it out.

Questions asked in this year's survey included topics such as why they selected Laravel, size of projects, if they're able to find the developers they need and what they'd like to see more of in the Larvel community (tutorials, articles, regional conferences, etc).

tagged: laravel survey results announcement 2018

Link: https://laravel-news.com/2018-laravel-survey-results

Liam Hammett:
Laravel Blade Helpers
Dec 04, 2018 @ 09:20:36

Liam Hammett has written up a post to his site covering the use and creation of helpers for Laravel's Blade templating. He shows the use of a package he's created to help make using them with custom callbacks simpler.

Laravel’s Blade templating engine offers a ton of convenient directives you can use to make your view files beautiful and abstract anything that may be too complex or verbose to live inside HTML. It even gives a really handy way to add your own custom directives using the Blade::directive(…) method.

However, the callback in custom directives only receives a single parameter?—?the raw string expression from the view file. [...] As this seems to be the most common use case, I put together a package that attempts to help make these helper functions that little bit easier to define without the boilerplate of returning the string or having to consider what an expression may be when creating a directive.

In his package he introduces a new method that defines the name of the method and the name of the function to call. This second option can also be a custom callback function, making it even more flexible.

tagged: laravel blade template helper package custom callback tutorial

Link: https://medium.com/@liamhammett/laravel-blade-helpers-8d710fa31fd9

Laravel News:
Unwrapping array_wrap()
Nov 30, 2018 @ 09:34:32

On the Laravel News site they have a new post that "unwraps" the array_wrap helper that makes it easier to normalize values into arrays and removes the need for manually checking if a variable is an array or set before using it like one.

Laravel has a wrap method and array_wrap() helper to normalize values into an array. Raul @rcubitto shared this nice tip about it on Twitter and before seeing his Tweet I wasn’t aware of this method.

They include an example of the helper function in use and share some of the responses to his tweet including questions of how it's different than casting to an array. They answer this by talking about more complex values and how the helper is useful for null values and creating consistency in your application.

tagged: laravel helper function array arraywrap tutorial

Link: https://laravel-news.com/array_wrap

Freek Van der Herten:
Building a realtime dashboard powered by Laravel, Vue, Pusher and Tailwind (20
Nov 21, 2018 @ 09:27:21

Freek Van der Herten has a tutorial posted on his site sharing the 2018 edition of how to build a dashboard powered by Laravel, Vue, Pusher and Tailwind CSS. It's based on a project they use internally to keep up to date on current work, events and even music people are listening to.

At Spatie we have a tv screen against the wall that displays a dashboard. This dashboard displays the tasks our team should be working on, important events in the near future, which tasks each of our team members should be working on, what music they are listening to, and so on.

[...] This dashboard is not entirely new. The initial version was published a couple of years ago. At our company, we like to tinker with new stuff. That's why we perform a big cleanup of the source code every year. We also update all the dependencies and technologies used. This year we focused on updating the design. The dashboard now uses Tailwind CSS. In this blog post, which is an update to the previous one, I like to explain how this dashboard is built.

He starts with a bit of history of the project and a look at the UI before diving into the code. The remainder of the (lengthy) tutorial is then broken down into functional pieces, each with code and explanation:

  • the creation of the markup for the grid
  • a closer look at the "internet connection" component
  • a closer look at the "statistics" component
  • the use of "dark mode" depending on the sun being up
  • displaying sites that are down

There's also a mention of the security requirements involved in the creation and use of the dashboard, the hardware used to show it on the TV and how data is saved to prevent the need for the initial "reboot" of the data.

tagged: realtime dashboard tutorial laravel vuejs pusher tailwindcss

Link: https://murze.be/building-a-realtime-dashboard-powered-by-laravel-vue-pusher-and-tailwind-2018-edition

Laravel News:
Building a Laravel Translation Package – Wiring Up The Frontend
Nov 16, 2018 @ 13:33:36

The Laravel News site has posted the latest in their "Building a Laravel Translation Package" series of tutorials with a new post focusing on the frontend and getting it connected to the backend functionality.

In the last installment of this series, we talked through the process of manipulating the translations in our application’s language files into a format where we are now in a position to start interacting with them. In this article, we’ll be wiring up the frontend ready to start building out the user interface which will aid users with the process of translation management.

The UI will be developed using the community favorites, Tailwind CSS and Vue.js.

The tutorial starts with the approach for the functionality, taking a more hybrid approach and using a balance of backend and frontend for the majority of the functionality. It then starts in on the technical parts to connect the two halves:

  • adding the required routes
  • creating the controllers
  • building out the views to render the interface

The majority of the post is then dedicated to the creation of the assets - the Javascript and CSS - to create the "framework" the functionality will use to work with the translations.

tagged: laravel tutorial series translation package backend frontend connect

Link: https://laravel-news.com/laravel-translation-package-frontend

Pineco.de:
Adding a Subdomain to Your Laravel Application
Nov 16, 2018 @ 11:44:42

On the Pineco.de blog there's a tutorial for the Laravel users out there showing how to set up subdomains in your application using the built-in framework functionality (and their setup with Valet/Forge).

Often we need to handle subdomains with our Laravel application. Let’s take a look, how can we treat the routes dynamically and set up the subdomain with Valet and Forge.

The tutorial then walks you through the steps to get subdomains working:

  • Defining Subdomains in Our Routes
  • Dynamically Generate Subdomains
  • Faking Subdomains in Valet
  • Setting Up Subdomains in Forge

Each section comes with a brief explanation and any code or configuration changes that would need to be made to make it all work.

tagged: subdomain laravel tutorial route dynamic valet forge

Link: https://pineco.de/adding-a-subdomain-to-your-laravel-application/

Tim MacDonald:
Loading Eloquent relationship counts
Nov 12, 2018 @ 09:51:30

Tim MacDonald has a new post to his site sharing methods that the Laravel Eloquent users (either in the framework or outside of it) can use to load in the counts of relationships without having to fetch the entire relationship data set.

It is often useful to show the number of related models a given instance has, but not actually need any specific information about the related models, just how many exist. In this scenario you do not want to load all of the related models into memory to count them, we want our database to do the heavy lifting for us. Laravel offers a number of ways to retrieve relationship counts. 2 have been around for a while, but there is a new kid on the block.

He looks at three methods you can use to get these counts: via the query builder manually, directly on the relationship and, more recently added, from an eloquent collection. He goes through each of these methods, providing a summary of the technique and code examples showing how it's implemented.

tagged: laravel eloquent relationship count tutorial querybuilder collection

Link: https://timacdonald.me/loading-eloquent-relationship-counts/

Adelf's Blog:
Are you sure you need entrust or laravel-permission to implement your authorization?
Nov 07, 2018 @ 09:33:19

In a post to his site Adelf aims to help you answer the question of the use of either the Entrust or Laravel-permission packages in your Laravel application to handle your authorization.

"Hmmm, I need some basic authorization, like admin role for admin panel and maybe some editor/moderator role... Let's google it. Wow! Laravel already has packages for that! zizaco/entrust, spatie/laravel-permission and others! Let's choose one!"

That's how it usually happens. Then package's migration will add about 5 tables to store roles, permissions and their relations. [...] It only looks simple: just install package, run ready migration and go on. From long-term point of view for 90% projects it's not the best choice.

He starts with an example of a common situation for most web developers: the inclusion of the package, the database changes that come with it and the management of keeping the permissions in sync. He makes the suggestion that maybe this seemingly "simple" way may not be the best and that, in most cases, they're overkill for what an application needs. He shows how to slim down this functionality using Laravel's own gates/policies and boil it down to just the checks that need to be made without the package overhead.

tagged: tutorial laravel permission entrust package requirement simple

Link: https://adelf.pro/2018/authorization-packages

Laravel News:
Building a Laravel Translation Package – Wrangling Translations
Nov 05, 2018 @ 11:53:20

On the Laravel News site they've continued their "Building a Laravel Translation Package" series of tutorials with the latest article covering the "wrangling" of the translations themselves.

As we’ve discussed earlier in the series, out of the box, Laravel translations are stored in language files. These can be either PHP array-style syntax or straight up JSON files.

[...] The plan for the package is, much like many features of Laravel, to expose multiple drivers to power the translation management. The first driver will utilize Laravel’s existing file-based translations with plans to later add a database driver.

[...] The file driver needs to interrogate the filesystem in order to return the data in the required format. This involves a lot of filtering, mapping and iterating, so we will lean quite heavily on Laravel’s collections.

The tutorial is then divided up into a few different parts, each for a chunk of the package functionality around translation management:

  • listing languages
  • adding languages
  • listing translations
  • adding/updating translations

Each item in the list comes with a summary of the feature and a few lines of code showing how to implement it.

tagged: translations laravel package series management tutorial

Link: https://laravel-news.com/wrangling-translations