Laravel Watchable Package

I created this package to ‘scratch my own itch’. I have a model called Order. I wanted to notify certain people (only people who were watching the order) when the status of the order changed.

In this article, I will give you some boilerplate code that you can use in your application to wrap around the Laravel Watchable package.  If you fancy reading a little more about why I needed to build it in the first place, read the “A little background into ‘Why'” at the end of the page.

The problem

In the documentation, under the Using The Notification Facade section, you can see the example below.

However, the question is, how do you get the users? What I want to be able to do is send a collection of users who are watching the model.

The solution

Install the Laravel Watchable Package (https://github.com/jamesmills/watchable)

Remember to add the trait to your model. Throughout this post, I will use Order as the example.

Once you have the package installed this will help you handle the watchable request on the model. However, you will still need to add some additional functionality to your application. Below is some scaffolding which I hope will help.

I have created a separate controller to deal with this so that I can reuse the same controller for any models I want to be ‘watchable’.

To use this controller remember to add the route!

Now you can use the example below in your view file when you are showing the model you want to be watchable.

Important: The key thing to remember is that we send a hidden field with the ‘key’ as the model name you want to watch/unwatch. This is what makes our controller code reusable.

Now that we have added the ability for someone to be able to watch your model we can simply get the watchers of the model when we perform an action on it.

The below is a simplified version of what we have in our application when an order gets paused.

Notes

I use the Laracasts Easy Flash Messages for the flash()->success(‘message’) messages you can see in the controller to display the watching status back to the user after they have clicked the button.

A little background into ‘Why’

At Clicksco we are experts in traffic acquisition and we use this skill to drive traffic to highly optimised landing pages to generate form fill leads for a number of partners focusing in the financial industry.

We partner with a company called Lead Byte for lead processing and delivery which we use in combination with a number of in-house systems to manage the entire lead lifecycle.

A few months ago I was working on a new lead ordering module which would allow us to create an order for leads and have the system manage it. A lead order is when a company comes to us and says they will buy X number of leads from us. They might require people looking for Y product and the value of their fund might have to be above Z.

Within Lead Byte we set all the delivery configurations so we can delivery the lead directly to the clients CRM. We also set up rules to make sure the correct partner gets the correct leads. The one thing we were missing was being able to create a lead order to fit various scenarios. (Example: Send 
20 leads daily until a total of 500 leads have been sent or until 22nd Oct 17).

Using a combination of Laravel, Pusher and Vue we implemented our solution in our main system (we call this the PMD). What was missing was a way for us to notify specific people when certain things happened to an order. For example, when the daily frequency was met and the order was paused. Or, when the total number of leads for the order was delivered so the order is closed. Using Laravel 5.5 Notifications it’s easy to send out these notifications using a selection of different channels but the tricky part comes when you need to decide who to send the notification to.

The is why the Laravel Watchable package was developed. I allow anyone who can log into the system to ‘watch’ an order. Once they are a watcher of a lead order they will automatically get all notifications of all order status changes. It’s easy to find the order, get all its watchers and then send a notification to everyone who is watching.

 

Open a php project in PHPStorm using Alfred

I have been using PHPStorm for a while now and after the recent release of version 5 (seems to be much faster) I am sticking with it for the foreseeable future.

I have also been using Alfred for a long time too. I purchased the Powerpack to get access to some of the more advanced features and have never regretted the small expense.

Today I found out Syd Lawrence has built an awesome plugin called Alfred Dev Doctor which helps developers find the correct documentation whilst coding. It needed Alfred v2 and the Powerpack.

I did not know there was a version 2 avaliable so I immediently downloaded the new version. Unfortuntly I then found out that my licence key for the version 1 Powerpack did not work with version 2 so I had to upgrade my Powerpack licence. The upgrade was a small fee of £10, worth it in my eyes. Since 14 May 2012, I have used Alfredapp 6,591 times, which is an average 20.9 times per day.

Since I had the all new singing and dancing version of Alfred and the Powerpack I began looking at other things I could get him to help me with. For months I have been trying to get Alfred v1 to open a folder with PHPStorm. I was never able to get this working. Luckily in Alfred v2 this was easy.

I have created a Workflow in Alfred which allows me to hit ‘alt+space’ then type ‘phps’ and then type a folder name to find it. Once it has found it I simply hit enter and it will open that folder up in PHPStorm. Yay!

A video of this working can be found here: Video showing Alfred opening a folder in PHPStorm

You can download the Workflow here, unzip and double click to install: PHPStorm Alfred Workflow

World cities database

I am currently working on a project that is focused around locations. The main thing I need to know is the City that the user is either in or wants to browser information about. It is a mobile website so I am giving the user a couple of options how to pick their location.

On the page where the users selects the location the first button is to allow them to use the browser to set their location ‘Use my precise location’

There is another button that uses a IP geolocation script which should say ‘Cityname’ which should be the city in which the user is.

The third option is to list a load of cities for the user to click to choose.

I am saving the latitude and longitude to be used throughout the application. The browser will return this, the geoIP will return this so I needed a way to get the latitude and longitude for each city I wanted to list. I started buy using Google APIs which worked well. I stored this information in a database table. The problem was that I wanted to give the user as many options as possible and I wanted to use cities in the US. I went on the hunt for a database and successfully found a couple of resources which had exactly the information I needed. One of them even had population so I was able to use this information alongside our other background research to influence our decision on which cities we would actually list so we were not trying to list all of them!

I thought I would share these two links are they will probably be of use to me in the future and I hope this will help someone else out.

Thanks to Brian Suda and Rick Trotter for the replies to my original Tweet.

A little something extra: Thanks to a Tweet from Philip Poots I have recently found out that if you append .json to the end of any gov.uk page you will get the information in json format! For example, if you wanted to know all the UK bank holidays then you can find this information at https://www.gov.uk/bank-holidays and if you add .json to the end… https://www.gov.uk/bank-holidays.json! This is awesome!

UK Postcode database with lat & long

As we all know the full postcode database with all latitude and longitude information is Copyright to Royal Mail. This is annoying. I know there are loads of databases out there which claim to be the full dataset and there are loads of API’s which will geocode UK postcodes etc. In the past we have used an awesome service called PostcodeAnywhere which does a great job of address lookup and validation… but it’s a paid service… obviously they have to make money somehow! I have also played with Google API etc. I also think that someone released a dataset on Wikileaks but if I remember rightly this was partial postcodes (only contains the first part of the postcode e.g. TS19).

I was chatting to Alex Akass on skype the other day and he pointed me to the Ordnance Survey website who have just released a full set of UK postcodes in a database as recent as February 2012! The service, called Code-Point Open (ordnancesurvey.co.uk) contains 2,993,351 rows. This data also looks to be available on the new data.gov.org beta site under the same name Code-Point Open (data.gov.uk).

The data comes in CSV files to split the data up. I created a quick PHP script that looped through these files and put all the postcodes into one database table. All looks good.

This got me thinking. The database comes with ‘Postcode’, ‘Eastings’ & ‘Northings’ columns. There must be something you can run Eastings and Northings through to get the latitude and longitude. I did a quick Google and found a post by a guy called samjudson on this forum.

I searched for my postcode in the database and then ran the Northings and Eastings through this function to get the latitude and longitude. I used itouchmap.com to load this data on the map and compared it with the pointer that got shown on the map when I inputted my postcodes directly. They were very close, close enough to be useful I think.

So what I was thinking, was I could add the conversion function to my php script and whilst I am looping through the csv files I could also add the latitude and longitude and I will end up with a current table with full UK postcode, Eastings, Northings, Latitude & Longitude.

Would welcome thoughts…

Printing an array, string or anything else…

This post is carrying on the theme of sharing little bits of code (like my good friend @boxedfish did the other day) – How to easily split a sequence of number into a hython seperate string

This is not the greatest bit of code I have ever written and its not going to knock you over with complexity but what it will do is save your little fingers and over a year give you back quite a large chunk of time.
[php]

function output($my_array, $my_die = 0)
{
echo ‘<hr />’;
echo ‘<pre>’;
print_r($my_array);
echo ‘</pre>’;
echo ‘<hr />’;

if ($my_die == 1) { die(); }
}

[/php]

Then all you have to do is

[php]
output($my_string);
output($my_array);
[/php]