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

Laravel News:
Leverage Eloquent To Prepare Your URLs
Apr 18, 2018 @ 11:44:13

The Laravel News site has a quick tutorial posted showing you how you can use Eloquent functionality to help prepare your URLs and make them easier to maintain across the application. The key is in the use of "presenters".

It’s not uncommon to have tens, if not hundreds of views in a Laravel application. Something that soon gets out of hand is the various references to routes. [...] If for whatever reason we have to make a change to either the route alias or default query string values you’ll soon find yourself doing mass string replacements across your entire application which brings the risk of breakage within many files.

What can we do to possibly better handle this? There are a couple of different approaches.

They provide two approaches, one being slightly more complex (but flexible) than the other. The first makes use of only Eloquent to define a getUrlAttributes method in the model. The second method abstracts this functionality out to a "URL Presenter", a class that defines methods for each of the CRUD actions and returns the correct route for each. The getUrlAttribute then returns an instance of this instead, making it easy to reference the method and route required in the Blade template.

tagged: laravel eloquent prepare url tutorial presenter

Link: https://laravel-news.com/leverage-eloquent-to-prepare-your-urls

Laravel News:
20 Laravel Eloquent Tips and Tricks
Apr 16, 2018 @ 09:28:33

On the Laravel News site there's a new post sharing twenty Eloquent tips for the Laravel users out there.

Eloquent ORM seems like a simple mechanism, but under the hood, there’s a lot of semi-hidden functions and less-known ways to achieve more with it. In this article, I will show you a few tricks.

Among the tips and "hidden features" mentioned are tips about:

  • Increments and Decrements
  • Model boot() method
  • Model properties: timestamps, appends etc.
  • Order by relationship
  • Order by Mutator
  • Raw query methods
  • Create additional things when creating a model

...and many more. There's code examples for each of them showing them in use too.

tagged: laravel eloquent tips top20 list database orm

Link: https://laravel-news.com/eloquent-tips-tricks

Michael Dyrynda:
Sharing databases between Laravel applications
Apr 13, 2018 @ 09:39:16

Michael Dyrynda has a new post to his site showing the Laravel users out there how to share a database between your applications. In his case one of the applications is a legacy app and the other is a newer Laravel application.

As a contractor, I had a sanitised copy of the database, and I managed to reverse engineer the Eloquent models from the database schema, creating factories along the way, in order to be able to write tests for the members application.

In late 2017, we started migrating our CRM to Laravel as well, in order to modernise the code base a bit, give it a standard structure, and make it easy to make changes to it moving forward. Now that we had two Laravel applications, we started looking at how best to share data between them.

He starts by talking about reverse engineering the models from the database structure and the use of migrations to manage the database schema. In the end he created a stand-alone tool, Nomad, that helps to keep things in sync between the two databases. He includes examples of it in use and how it helped to keep the database in sync despite permissions issues and connection problems. He also mentions how they used it to take care of some testing issues, database configuration changes and how to use it in a continuous integration pipeline.

tagged: share database laravel application nomad tool migration schema

Link: https://dyrynda.com.au/blog/sharing-databases-between-laravel-applications

Laravel News:
Sending a daily email with Laravel and Campaign Monitor
Apr 12, 2018 @ 09:46:08

In a new tutorial on the Laravel News site, the author (Eric Barnes) shows you how to create a Laravel command that will send a daily email newsletter with the help of the Campaign Monitor service.

Here on Laravel News, we offer multiple ways of staying up to date with new content. Everything from auto-sharing to all the social media channels, a read-only Telegram channel, a weekly newsletter and last March we started offering a daily email digest.

To send the daily email we utilize the Laravel scheduler and Campaign Monitor so it’s completely automated. In this tutorial let’s look at how its all setup and how you can easily add this to your site to start sending out automated emails.

The tutorial starts by helping you get the Campaign Monitor SDK package installed (via Composer) and configured to use their API with your own key. Next it walks you through the creation of the console command to hook into the Scheduler for automated sending. It includes the code you'll need to find the data (in this case the latest blog posts) and the email method to send the information to the Campaign Monitor API. There's a brief mention of the creation of the HTML for the newsletter based on the Zurb Foundation and the configuration change to add the command to the scheduler for execution.

tagged: laravel tutorial command newsletter automation campaignmonitor api

Link: https://laravel-news.com/laravel-campaign-monitor

Laravel News:
Going Deeper with Factories Through Factory States
Apr 11, 2018 @ 10:11:58

The Laravel News site has posted a tutorial that takes you deeper with factories when using the framework looking at the factory states.

I suspect that if you are familiar with Laravel that you might use factories and possibly even factory states in your application development. The documentation shows you the mechanics of using factories to seed and create test data, but there are a couple of guiding thoughts to using factories with your models effectively that I’d like to consider.

He starts with two options he sees for using factory states: making them with static values or using them to make simple models instead. He goes through these two options, introducing some of the basic concepts of each, how it would work and the code to make it happen. He also covers a few other approaches including the use of a trait to include the factory functionality directly in a class. He finishes the post with a few links to more information in the Laravel manual and other outside resources/tools.

tagged: laravel factory state static value model tutorial

Link: https://laravel-news.com/going-deeper-with-factories-through-factory-states

Adnan Sabanovic:
How to use Laravel with Socket.IO
Apr 09, 2018 @ 12:07:28

In a new post to his Medium.com site Adnan Sabanovic shows you how to combine Laravel and Socket.io to create in-application messaging.

Websockets are cool. They are really helpful if you want to show real time activities from your users (or perhaps some queue jobs). Now, if you are afraid of the word “Websockets”, don’t be. I will lay down the instructions on how you can use it and will be around to answer your questions if you need to.

I had this challenge where I needed it to show a list of people who are currently viewing a specific URL in Laravel. So I started thinking. Part of me wanted to do a quick hack (luckily that’s not the strongest side of mine), whilst the other wanted to build something cool, reusable and long-lasting.

While many recommend using the Pusher service, there's a way to replicate it locally using Socket.io and the Laravel Echo Server. He walks you through the process of getting a new Homestead box up and running, installing Redis and installing the Echo Server. After some configuration changes he shows the installation of the Socket.io client and laravel-echo interface. Example code is included showing how to send the broadcast messages and to listen for broadcasted messages.

tagged: laravel tutorial socketio broadcast echoserver

Link: https://medium.com/@adnanxteam/how-to-use-laravel-with-socket-io-e7c7565cc19d

Laravel News:
Using the Laravel Optional Helper and the New Optional Closure
Apr 09, 2018 @ 10:14:07

On the Laravel News site there's a new tutorial posted showing you how to use the "optional" helper and closure to help control issues where a property or method doesn't exist (or can't be accessed).

The Laravel Optional class and accompanying optional helper were introduced in Laravel 5.5. This class is useful to avoid those pesky Trying to get property of non-object errors in your code.

Joseph Sibler submitted an improvement to the Laravel optional helper that we covered in Laravel 5.6.13 which now allows a closure that is only called when the object is not null

They compare the "optional" functionality to the null coalesce operator and the cases when the former should be used over the latter. A few code snippets also help to illustrate the difference. For more information about this helper and other handy features, check out their other article with a "top 5" list of their favorite helpers.

tagged: optional helper closure tutorial example laravel

Link: https://laravel-news.com/laravel-optional-helper

Laravel News:
Laravel Live UK (2018)
Apr 06, 2018 @ 10:09:06

On the Laravel News site today there's an announcement about the next Laravel Live UK conference happening this coming June (2018) in London.

Laravel Live UK is a brand new one-day conference that will be held on June 12th, 2018 at The British Library.

Confirmed speakers include Matt Stauffer and Lorna Jane Mitchell. They’ve also just opened up CFP, and are specifically looking for talks focused on diving deep into Laravel and the Laravel ecosystem.

The conference already has tickets on sale for this single-day event too. The Early Bird tickets are already gone but you can get a General Admission ticket for £100 or a last minute ticket for £125.

tagged: laravelliveuk18 uk laravel conference london june

Link: https://laravel-news.com/laravel-live-uk

Laravel News:
Laravel Route Tips to Improve Your Routing
Apr 05, 2018 @ 11:13:12

On the Laravel News blog they've posted a tutorial with some helpful tips for improving your routing in your Laravel-based application.

The Laravel router has a great, well-polished API when you first dive into Laravel as a beginner or newcomer to the framework. What follows is not anything hidden or new, but a few tips that should help you when you’re learning Laravel 5.

The documentation is excellent, and the tips that follow supplement and piece together a few parts that will help you get a quick jumpstart on learning how to use routing in your Laravel applications.

The tips include:

  • Custom Namespaces
  • Route Macros
  • Debugging Routes
  • Named Group Routes

Each of the tips include the code needed to implement them and a brief summary of why they're useful.

tagged: routing tips laravel tutorial namespace macro debug named

Link: https://laravel-news.com/laravel-route-tips-to-improve-your-routing

Laravel News:
Learn to Create an RSS Feed from Scratch in Laravel
Apr 04, 2018 @ 10:47:52

On the Laravel News site there's a new tutorial showing you how to easily create an RSS feed with Laravel and the help of the spatie/laravel-feed package.

Creating an RSS feed in Laravel isn’t the most challenging task, but using a package and a few tips can help you create an RSS feed relatively quick.

We are going to use the spatie/laravel-feed package to walk through going from a brand new Laravel 5.6 project to serving RSS feeds.

The tutorial then goes through each step along the way, providing code and commands to get you up and running:

  • Setting up a new Laravel project
  • Modeling the data
  • Setting up the (event) model
  • Creating the Routes and the Events Controller
  • Linking to the feed

The result is a simple RSS feed that can be linked to from a link tag in your site and provide a list of upcoming events.

tagged: laravel tutorial rss feed spatie package

Link: https://laravel-news.com/learn-to-create-an-rss-feeds-from-scratch-in-laravel