data codes through eyeglasses

Log Laravel Eloquent SQL queries with parameter values

Frequently I find the need to see what SQL queries are actually generated magically by Eloquent. It really helps when I need to debug a query or figure out why its not performing well. Since I keep referring back to this very useful code snippet I thought its good to keep it here so its readily available for myself and anyone else. Add the following code in the boot() method of AppServiceProvider.php:

    public function boot() {
if (\App::environment('local')) {
DB::listen(
static fn($query) => logger(
Str::replaceArray('?', array_map(fn($binding) => '\'' . $binding . '\'', $query->bindings),
$query->sql
)
)
);
}
}

If you haven’t changed Laravel’s default logging configuration all queries executed will be logged in storage/logs/laravel.log. The full query including its parameter values will be included which makes it dead easy to then copy/paste and run EXPLAIN so you can see how you could optimize it. Note that this will only execute if APP_ENV is set to local so no sensitive information will be logged on your production logs.

PHP 7.4 Preloading

A cool new feature coming up with PHP 7.4 will give a nice speed boost while solving one of the problems that hold PHP back. As explained in the internals RFC we’ll be able to specify a single PHP file in php.ini (under the opcache.preload directive) which will take care of loading all other PHP files that contain shared code and keeping them in memory before any application code is executed. The preloader PHP script can either use the include command the traditional way or the new function opcache_compile_file to force scripts to be compiled and kept in memory. It will also resolve any dependencies and load them accordingly which wasn’t possible before using the opcache. The shared code will then be available to any PHP scripts running on the same instance without having to do any include/require or pass through composer’s autoloader.

Performance gains from this feature will depend mainly on how much actual work is done during each request as opposed to bootstrapping the framework. Requests will short run-time should see the highest boost in performance.

While this feature will benefit anyone running their applications isolated it will probably not be available for shared hosting scenarios where the same PHP instance is used for multiple sites.

PHP 7.3 is out

Can be downloaded here http://php.net/releases/7_3_0.php

I personally would stick to 7.2 for now at least until the first point releases of 7.3 are out just to be on the safe side.

To install on Ubuntu 18.04:

sudo add-apt-repository ppa:ondrej/php
sudo apt-get update

deb http://ppa.launchpad.net/ondrej/php/ubuntu bionic main 
deb-src http://ppa.launchpad.net/ondrej/php/ubuntu bionic main

sudo apt-get install php7.3

Xdebug is also available but its currently under beta. Since you won’t be using that in production it doesn’t hurt to try.

Recent & Worthwhile in Web Development

PHP

  • Remember Me – Safely Secure Long Term Authentication Strategies
    A detailed explanation on how to implement the Sign in ‘Remember Me’ functionality securely in PHP (the concepts are the same for every language). I must admit I never considered timing attacks for the authentication token until I read this.
  • Laravel 5.0 was released
    The 5th installment of one of the most popular PHP MVC and all-around frameworks was released. Wrote a summary of the changes in another post.
  • HHVM 3.5.0 Released
    The Facebook supported alternative PHP VM has a new version.
  • Cockpit
    A plug-n-play CMS for PHP sites. Targeted to developers, who built web apps and don’t want to re-invent the wheel every time for the content parts of their app. Uses SQLlite or MongoDB as data store.
  • Return Types Declarations approved for PHP7
    In PHP7 we’ll be able to declare the return type for functions and will be enforced causing a fatal error if a different type is returned.  Here’s an example of how it will look taken straight from the RFC:
    [pastacode lang=”php” message=”” highlight=”” provider=”manual”]

    function foo(): array {
      return [];
    }

    [/pastacode]

JS /HTML / CSS

  • ReactJS for Stupid People
    A good introduction on what Facebook’s ReactJS is about and why you should use it.
  • Animated Bezier Curves
    Demonstration of how parametric Bezier curves are constructed. Uses D3, and the code is straight forward, just View Source.
  • Konva JS
    Object-oriented 2D HTML5 Canvas Framework. Event driven, layer support, drag and drop, shape nesting, animation and all the usual bells and whistles.
  • mProgress.js
    Material Design style animated progress bar. Uses CSS3, has no other library dependencies and has beyond the ordinary features such as indeterminate progress (7.7kb minified JS).
  • RubaXa Sortable
    Minimal & customizable Sortable with touch support and no dependencies.
  • fieldVal
    Programmatic Javascript Field Validation library (runs both on browser and server). Includes an optional UI library for presenting validation errors in forms.
  • TauCharts
    Javascript charting library which aims to be flexible rather than offering pre-packaged chart setups.

Online Shops that ship to Cyprus

Its extremely frustrating to find a product you want to buy online only to realise they don’t ship to your country (and to do so, sometimes you have to go through the process of filling up 3 pages of sign up forms). Living in Cyprus which happens to be too small to be a viable marker for many its very rare to find online stores that care to send their products all the way to us. So I made the following list of places that I’ve either ordered before or I know that ship to Cyprus. I plan to keep this list updated as I find new Cyprus-friendly shops – and if you know any I missed, please let me know. So here’s my list:

 

Site Ships from Products & Notes
Play.com UK (and more) Video games, Books, BluRay/DVD, Mobile accessories, Limited selection of clothes. Sells other products too but doesn’t ship to Cyprus.

Used to ship to Cyprus for free when it was a single shop but now it became a reseller platform so some sellers will send for free some for a small fee. But in general they do send to Cyprus without a problem.

Game UK UK Video Games, Console accessories.

Good prices, has new games on time. No free shipping but cost with shipping usually comes about the same as Play.com

Zavvi UK Video Games, Movies and limited range of computer products/laptops.

Quotes prices in Euro and has free shipping to Cyprus via Post for most items. Appears a bit more expensive than Game.co.uk on the items I checked but might be compensated by the free shipping.
(Thanks to Dimitris Georgiou for the tip)

Overclockers UK UK Laptops, Desktops, PC Components.

Best shop for hardcore PC gamers. Has custom built gaming machines, and a great selection of high end PC components. If you are building a powerful PC, you should look here first. Prices are OK (you can probably find cheaper if you look elsewhere). They might give you some trouble when ordering from Cyprus, in the past I was asked to send a scan of my passport, but after that they sent my order without any delays.

Computer Universe Germany General Electronics, Laptops, Mobile Phones, Tablets, Cameras, Lenses, PC Components, HiFi.

Only found out about this store recently and haven’t had the chance to order from them yet. But compared to other sources they seem to have the cheapest PC components and a good selection of them. Also their laptop section has almost everything you’ll need. They ship using regular post (DHL in Germany but you’ll receive your product in the Post Office in Cyprus) or UPS which costs considerably more.

B&H Photo USA Everything Photography Related, Laptops, Tablets, Professional Audio & Video, TVs and Projectors.

One of the few US based shops that will ship any of its products to Cyprus. If you are interested in photography this is the site to check. They sell cameras, lenses, tripods, lights, filters accessories and anything a photographer might need, from the cheapest snap cam to the most expensive and rare SLR Lens. The prices are good you will have to pay taxes in Cyprus since they ship from outside EU, so in some cases check amazon.co.uk or ebay.co.uk for the same products before ordering. They ship via UPS or Air Freight for large items such as TVs (which can be costly). They also offer the option to prepay taxes through their site. Personally I never used that option and it turns out you pay a little less tax in Cyprus.

Adorama USA Photography, Laptops, Tablets, Professional Audio & Video, TVs and Projectors.

Similar product range with B&H Photo, ships to Cyprus via UPS.

Clove UK Mobile Phones, Tablets & Related Accessories.

Never used them myself but I know people who did and are quite happy. They have the latest handsets in stock almost immediately after release (usually earlier than Cyprus retailers) and their prices are quite good. In addition to credit cards they also support PayPal which is a plus.

HandTec UK Mobile Phones, Tablets, GPS.

Similar product range to Clove, but appears to be slightly cheaper (at least for Samsung and LG models I checked).
(Thanks to Michael Demetriou for the tip)

Please note I am not including any Chinese stores, since there is literally hundreds (if not thousands) of them that usually sell the same products via drop-shipping and they all super happy to ship to Cyprus (usually for free via regular post). I might write another post about Chinese shops in the future.

I also don’t include Amazon.co.uk because a lot of its sellers won’t send products to Cyprus.

If you know of any other shops that you think should be included in the list above please post about them and your experiences in the comments.
 

In search for the best Cloud Storage

The past couple of months I started receiving warnings from Dropbox that my ‘gift’ 50GB space that came with the purchase of a Samsung Galaxy S3 2 years ago is coming to an end. I am left with around 20 days so I started looking at the alternatives because Dropbox is pricey. I already have accounts with Google Drive and Microsoft’s OneDrive and I also checked out Box at some point but never really used it. For Google Drive I have an 80GB plan which I signed up for years ago when Picasa Web Albums was still alive (it still is but I have the impression Google will pull the plug soon).

Just today OneDrive announced some big price reductions making it more competitive to Google Drive.
Here’s a table of the current pricing of the storage providers I am considering:

Annual cost for 100GB

Dropbox GoogleDrive Box OneDrive
$99.00 $23.88 $120.00 $23.88

Since Box is more expensive while not offering anything better than Dropbox I am no longer be considering it. Since I am currently using Dropbox I won’t need to do anything other than just buy more space. A great feature of Dropbox which I would miss if I was to move to DropBox is its automatic photo uploads for mobile devices. Any photos taken on mobile are automatically saved on my Dropbox and then automatically downloaded to my PC, keeping them both safe and available without having to do any manual transfers. Google Drive has a similar feature but with a catch; instead of saving photos to Drive it sends them to Google+ which means you don’t have direct file access to photo folders. If you want to download them you have to browse your Google+ profile and download them one by one, a very painful process. There is a third-party Android app which does automatic photo upload to Google Drive but I haven’t tested it yet – the fact that I will need to run another app in the background which could drain more battery worries me. OneDrive is supposed to have an automatic photo upload feature more in the lines of Dropbox than Google but I haven’t tested its Android app yet. It comes built-in Windows 8.1 and all Save Dialog default to OneDrive which is convenient for a Windows user like myself. Microsoft was  wise enough to release apps for all platforms so Android is covered. Another great feature of OneDrive is that unlike Dropbox, files stored in the cloud do not necessarily require to have a local copy. The OneDrive app ties in with Windows and creates place=holder files for large files and only downloads them when they need to be opened. This is especially useful for users on SSD drives which tend to have limited capacity.

Until today I was leaning towards GoogleDrive but I am reconsidering and might give OneDrive a go. I’ll install OneDrive app for Android and see how it goes. In the meantime I believe prices will continue to fall because of competition between Google and Microsoft.

Google plans to compete on every price range – $99 tablets on the way

Apparently Google is planning to undercut the competition and add another price point for Android tablets. The rumor which is not far from reality is that Google will release a tablet by Christmas for 99 USD. Its not the first tablet to hit that price range, Chinese companies have been flooding the internet for more than a year with capable devices at that price point but this is the first time it will be backed by Google and carry the Nexus brand name.

The rumor says that the tablet will be based on a single core Chinese SoC by WonderMedia Technologies and we can safely assume it wont have a screen larger than 7inch. It sounds under-powered compared to the current batch of Nexus 7 which runs on NVidia’s Tegra 3 Quad Core Soc, and this is coming from someone who already owns a couple of single core Chinese tablets. What we don’t know though is what optimizations and cut backs Google might plan to make to their OS to keep things running smoothly.

Either way if the screen is bearable and browser speed is decent for the cost of a night out you could own a tablet with full Google services support.