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

Laravel News:
Upcoming Laravel Events and Meet-ups happening in July
Jul 09, 2018 @ 09:55:46

On the Laravel News site they have a new post sharing some of the Laravel-related events happening in July (2018).

Looking to meet other Laravel developers? Want to improve your skills as a developer? Consider attending a meetup or a conference in your area.

They have four events listed for this month:

Dates and links to each are provided in the post including a link to the Laravel Events calendar.

tagged: laravel events july2018 meetup conference

Link: https://laravel-news.com/events-july-2018

Pineco.de:
Implementing Custom Logic With Raw SQL In Laravel’s Query Builder
Jul 04, 2018 @ 12:19:35

The Pineco.de site has a new tutorial posted showing you how to implement custom logic with raw SQL in the Eloquent query builder in the Laravel framework.

Laravel’s query builder offers a nice way to work with raw SQL. We can use them in our where conditions and also in our orderings as well. Let’s see some examples where we can use raw SQL to implement custom logic for ordering the results.

The post starts with a brief mention of the difference between sorting and ordering results, noting that one happens on the SQL server and the other on the results collection. Next they show examples using raw SQL to order a query using both a simple and more complex condition. There's also a link to the official documentation for the raw methods for more information and examples.

tagged: custom logic raw sql laravel query builder tutorial

Link: https://pineco.de/implementing-custom-logic-with-raw-sql-in-laravels-query-builder/

Pineco.de:
A Deeper Look at Laravel’s Localization
Jul 03, 2018 @ 13:22:31

On the Pineco.de site they've posted a new tutorial covering the use of Laravel's localization functionality to more easily develop multilingual applications.

Laravel’s localization functionality is a huge help when we are developing multilingual applications. But still there are some less known features, so let’s take a more in-depth look to explore them.

First of all, we wrote an article about pushing the translation mechanism to your front-end. Also, we have a package that covers all the functionality on the front-end for you. Now in this post, we will cover the array based localization, not the JSON one. If you are more interested in the JSON based translations, you can find the docs here.

They start with some of the basics of the helper functions Laravel provides to work with localization of strings and how to use "translation parameters". It then goes on to show examples of pluralization handling and finishes by mentioning a few other features for returning more translation data and the @lang Blade directive.

tagged: tutorial laravel localization i18n deeper look

Link: https://pineco.de/a-deeper-look-at-laravels-localization/

Emir Karsiyakali:
Quick Tip - Logging Mastery with Laravel
Jul 02, 2018 @ 13:54:11

In this new post to his site Emir Karsiyakali shares a quick tip for the Laravel users out there to improve your logging output and make following requests easier.

Putting unique id to requests one of my silver bullet while designing RESTful APIs. It provides an extremely easy way to follow each request’s lifecycle while debugging. In this guide I’ll show you how you can add it to your log files on Laravel 5.6.

Under the hood, Laravel utilizes the Monolog library, which provides support for a variety of powerful log handlers. Laravel makes it a cinch to configure these handlers, allowing you to mix and match them to customize your application’s log handling.

He starts with adding the "tap" value to the logger configuration and pointing it to a newly created LocalLogger class. He shows the implementation of this logger class including a custom log formatter that will modify the log string to add the unique ID after the "datetime" value. He also includes some usage examples in your Laravel code, putting the Log::info line into a LogRequestResponse middleware to be executed on each request.

tagged: laravel tutorial log unique id monolog

Link: https://emirkarsiyakali.com/quick-tip-logging-mastery-with-laravel-7282988032a7

Dieter Stinglhamber:
Join the light side, we have no cookies.
Jun 29, 2018 @ 11:44:27

Dieter Stinglhamber has a new tutorial posted to his site showing you how to build a Laravel application that doesn't use cookies...well, any cookies that could be used for tracking.

Since May 25th you have been harassed by "We have updated our privacy policy" emails but also websites started to great you with "Please, let us and our 256 partners track you".

In response to these abusive practices, some developers have decided to follow a better path, removing every cookie that is not needed. For example, my website doesn't have a single cookie, Spatie new website won't have any either. [...] I encourage every developer to do the same and if you are using Laravel, here's how you can (very) easily get rid of the default cookies set by the framework.

He talks about the two cookies that are used by default in Laravel applications: the CSRF token and the session ID. He shares ways to remove each of these cookies (if you're not using them) and, for a final "trick", removing other cookie-related middleware from the "web" group that interact with cookies.

tagged: cookies laravel remove tutorial csrf session middleware

Link: https://dieterstinglhamber.me/join-the-light-side-we-have-no-cookies

Matt Stauffer:
Enterprise Laravel Help Me: White-Labeled Clothing Stores
Jun 27, 2018 @ 11:04:46

Matt Stauffer has posted the second article in his "Enterprise Laravel" series to his site. In part one he started with a practical approach to seeing how large the codebase is (counting lines). In this new post he takes an example situation (gathered from conversations with other developers) and shows how Laravel can fit its "enterprise" level needs.

Recently, I grew tired of hearing people say, "Laravel is fine for small apps, but when you are working in the Enterprise..." You can almost hear the capital E in enterprise, like they're talking about the starship.

I've spent the last few months trying to gather information about what makes people say this, and also stories of folks who are already successfully working with Laravel in enterprise context. I put out a <a href="https://enterpriselaravel.com/success-stories-survey>survey to collect stories, and wrote and delivered a talk (which I hope to give again soon) titled "Laravel and the Enterprise."

The idea, stemming from a submission via his "help me" form, is an application that lets customers create their own white-label storefronts for clothing. He starts with the submission itself and the issues it points out including:

  • the load caused by adding a new tenant (requires more servers)
  • a messy codebase
  • a more "monolithic" approach as it evolved away from simple CRUD

He then goes through each of the (five) points made and his responses trying to help mitigate the issue. Some of them come with more technical recommendations but for others the answer is more about documentation and processes.

tagged: enterprise laravel whitelabel clothing helpme advice application

Link: https://mattstauffer.com/blog/enterprise-laravel-help-me-white-labeled-clothing-stores/

Freek van Der Herten:
Breaking Laravel's firstOrCreate using race conditions
Jun 22, 2018 @ 09:46:52

In this new post to his site Freek Van der Herten shares a time when he was working on a data import that ended in some unexpected results thanks to an interesting race condition.

Recently I was working on a client project where a data import was performed via queues. Each record was imported in its own queued job using multiple queue workers. After the data import was done we had more rows than expected in the database. In this blogpost I'd like to explain why that happened.

He starts by digging into the code that made use of the firstOrCreate method in Laravel's Eloquent handling to find if an entry had already been created for the given data. The method uses two queries, one to determine if the record exists and another to create it if not. The issue was with the fact that it was being handled in a queue meaning that the select could happen and return false while another process was creating the record. He even created a demo app to show it happening and includes screenshots showing the result. He recommends moving the process to a separate queue and having only one worker executing at a time. There's not a good code-based solution for it as it's more of an issue with the architecture than the application itself.

tagged: laravel eloquent firstorcreate tutorial race condition

Link: https://murze.be/breaking-laravels-firstorcreate-using-race-conditions

Ben Sampson:
Creating a decent Laravel deploy script
Jun 15, 2018 @ 13:45:38

Ben Sampson has posted a quick guide on his site sharing a deployment script for Laravel applications he's found useful.

A good deploy script can save you time and speed up your application, and it only takes a few minutes to set one up.

I have a standard deploy script which I use for almost all of my projects, which I'm going to break down and share with you.

Note: This article assumes that you're using Laravel Forge. If you're not, many of the settings will be applicable, but the setup steps may be a bit different.

He starts with a brief description of what a deployment script is and goes through the functionality he has included in his:

  • switching the site to maintenance mode
  • clearing caches
  • clearing and optimizing routes
  • clearing and optimizing the configuration
  • clearing expired password reset tokens
  • compiling front-end assets

He puts it all together into a single file that's then included in the Forge deployment process. He doesn't provide other instruction as to how to use this script with another deployment method but it can potentially be used anywhere a bash script could.

tagged: laravel deployment script steps laravelforge tutorial

Link: https://sampo.co.uk/blog/creating-a-decent-laravel-deploy-script

Laravel News:
Factory Callbacks and Closure-Based Guards in Laravel
Jun 12, 2018 @ 10:40:36

On the Laravel News site, there's an article posted covering the use of factory callbacks and closures in guards in Laravel. This feature was snuck into a recent release and allows you to provide a bit more dynamic functionality to your models and guards.

Two undocumented (before today anyhow) features were recently added to the Laravel 5.6 documentation, and they are both fantastic!

The post details each of the two new additions starting with the factory callbacks allowing you to perform additional tasks when a model is made or created (code examples included). The guard changes allowing closures and the Auth::viaRequest method to define new guard drivers.

tagged: factory callback model closure guard laravel

Link: https://laravel-news.com/factory-callbacks-closure-based-guards

TutsPlus.com:
Set Up an OAuth2 Server Using Passport in Laravel
Jun 08, 2018 @ 11:51:36

The TutsPlus.com site has a new tutorial posted showing you how to create an OAuth2 server with Laravel and the help of the Passport library.

In this article, we’re going to explore how you could set up a fully fledged OAuth2 server in Laravel using the Laravel Passport library. We’ll go through the necessary server configurations along with a real-world example to demonstrate how you could consume OAuth2 APIs.

I assume that you’re familiar with the basic OAuth2 concepts and flow as we’re going to discuss them in the context of Laravel. In fact, the Laravel Passport library makes it pretty easy to quickly set up an OAuth2 server in your application. Thus, other third-party applications are able to consume APIs provided by your application.

The article is then divided up into the steps (code, configuration changes, and commands) required to get the system up and running:

  • Installation of Passport (requires a Laravel app already installed)
  • Changing the User model to add the "remember_token" field
  • Setting up some demo resources
  • Adding the middleware and routes to handle the requests

The tutorial then spends some time showing how to consume OAuth2 APIs and makes use of the Passport "client" to make some sample requests. Finally, it walks you through the whole process of the OAuth2 experience from a user perspective, including some code to manually make the connection from plain PHP.

tagged: tutorial oauth2 server laravel passport client token

Link: https://code.tutsplus.com/tutorials/setup-oauth2-server-using-passport-in-laravel--cms-30576