The following articles were authored by me-kono-office

How to validate Eloquent Models with dynamic patterns and multipe database connections

Loving Laravel and Eloquent Models

Well after some years sticking with Zend Framework, we at me’kono completely fell for Laravel as our main PHP Web development framework, which has a beautiful and easy to read MVC design pattern. One of the most frequently used Classes / Feature of Laravel is the Eloquent Class which – basically speaking – represents an entry in your database and adds methods and relations to it. One very important method is the save()  function obviously, which gathers all the data of the current object instance and tries to save it to the database.

Validation before saving

Since we didn’t want this method to simply run against the database and maybe return an error, the decision was made to have a validation before even starting the storage process. To make things more readable, the validation patterns are stored in a static array in the notation defined by the Laravel Validation Class:

To have these validation rules being applied each time the object gets stored to the database, simply override the save()  method with something like this:

And the isValid()  method we use comes here:

As you can see the method gets all attributes of the object, dynamically creates a Validator from the class’s validation rules and applies them. If validation fails the errors get stored in the object’s errors property. Hence you can better hunt down why storing the data ran into trouble.

Some special DB fields

Ok, this is pretty easy and straight forward, but occasionally you might run into the following problem:
Let’s pick up the common requirement that there should be no duplicate email address in your Users table, i.e. email should be unique. So you can change the validation rule to the following:

If you try this, it works fine if your user’s data is inserted once and then only read from the database, meaning you cannot update it. The eloquent update()  method also uses the save()  method. But since your email is already in the database, the logic just implemented prevents you from storing to the database.

But the guys from Laravel are clever and have taken care of this. You may specify an additional parameter for an ID, which should be ignored or is fine the have the same email (from the docs):

Make it dynamic

Almost there!

Just some additional thoughts: This way all your isValid()  methods have to be custom made for each model. And – you have to distinguish between save operations and update operations. So we came up with the idea of dynamic validation rules. The strings defining the validation are parsed before creation of the Validator and some variables of the object are filled in upon request (changes to above highlighted):

The syntax is dead simple: %[name of object property]$[printf output format]

The array_walk function  iterates over the validation rules and nvsprintf  applies the object properties to the validation strings.

Multiple DB connections and upgrading to Laravel 5.2

When we starting with Laravel, the framework stood at version 4, then we quickly moved to 5.1 – which is the first Long Term Support (LTS) Version of the framework and our project grew bigger and bigger. Now in some parts the framework is using two database connections – even in it model validation. So to search in the correct database for unique values, you have to define, which connection to use. In Laravel 5.1 this may look like this:

We created the Eloquent object over a specific database connection ( User::on('my-other-db')->find($user_public_id); ) and before updating it to the database, the Validator has to be assigned the same database connection.

Now in Laravel 5.2 the validateUnique() method of the Validator Class changed reading the database connection from the defining string. So the database connection has to be set in the validation rules. Since we already had implemented dynamic validation rules this was easy to achieve:

And a slight change to our isValid() method:

 

me’kono gains further traction throughout october and november 2011

October and November brought me’kono a further boost in visitors and pages views, which increased by 105% (to 1,028) and 99% (to 2,143). During the period of consideration we had 159 conferences listed and 46 deadlines expired. me’kono could help to channel 58 submissions and registrations to these events—an increase of 107%. Our plattform showed 84 People the way to the right conference, up by 155%. So if you want to increase traffic and engagement for your conference, register today!

These are whopping figures and gives us extra motivation to better the me’kono conference plattform further. Hope, you saw our new sharing buttons on top of each conference abstract! Some under-the-hood changes have also been implemented. For instance Google search results will get much better soon, since me’kono pages are pimped with schema.org style microdata. What this means for you, you might ask. Quit simple – your search results will get much more informative and visually appealing. Just check out this one:

In the search results visitors are able to see location, date and description on first glance – much more convenient!

Finally, me’kono has now its own page on Google plus and we are promoting your conferences there as well! For december — besides getting more conferences online — we are preparing some serious website tweaks and some new year’s surprises. So stay tuned, tell us what you think and check back soon!

me’kono is starting to roll – site stats for september/oktober

me’kono started to kick in and is generating interest in the scientific community. To give you a idea how me’kono is performing, here are some statistics for mid-september to mid-oktober (15.09.11 – 15.10.11) period:

We welcomed 502 unique visitors, how read about 1,079 pages in total. The far largest group of visitors are english speaking (59%) – at least according to their browser request language. The most people came from the United States, although here the differences are very small (US: 10.5%, IN: 9.8%, UK: 6.4%).

What could be more important for conference organisors sighting up with me’kono are figures regarding submission and registration: from the 502 unique visitors, 33 went for additional information to the conference pages linked, 13 registered to the conference and 15 submitted either an abstract or a full paper. After considering the bounce rate, this means that about 1/3 of our visitors to a page actually engage in some manner with the enlisted conference.

And yes, I almost forgot it: by 15 October, me’kono had 112 upcoming conferences listed – and we are adding more day by day!

Can amazing jpegmini help shrink large Powerpoint Presentations?

Just recently I came across an amazing new webservice to be found at http://jpegmini.com. You simply upload your jpeg images and can download them after a short time, having them shrinked by up to factor 8. For people running websites with large background images or your personal cloud storage this is amazing! Loadtimes are dropping considerably and you can stuff more images into the same webspace. This service helps you save money and time – and it’s all for free!

Ok, so far this is really useful. But how about shrinking bulky Powerpoint presentations? With some effort and creativity it is absolutely no problem to create a decent presentation of 30 slides weighing in a hefty 5-10 MB. And then your conference host asks you to upload your file onto the university’s server / send it him per email. Will a combination of JPEGmini and Powerpoint help?

To cut it short: No! Powerpoint simply doesn’t care about image sizes and resolution. We tested it with the following setup: took John Picken’s beautiful image from Lake Como from jpegmini website and embedded into a Powerpoint in full size (about 6 MB) and the optimised version (about 1 MB) at a resolution of 3705 x 2829. The result was two files with exactly the same size of about 600KB. After exporting the image out of the ppt, it was clear what happened: Powerpoint simply shrunk the image both times to 974 x 774 (99 KB)!

To conclude, JPEGmini wount help you reducing presentation file sizes. And! Powerpoint is not suitable for large screen presentations – so no HD or similar output!