Invalid default value for Datetime type in MySQL

If you generated the script from the MySQL workbench.

The following line is generated


Remove TRADITIONAL from the SQL_MODE, and then the script should work fine

Else, you could set the SQL_MODE as Allow Invalid Dates


Difference between method calls $model->relation() and $model->relation

Short answer

$model->relation() returns the relationship object

$model->relation returns the result of the relationship

Long answer

$model->relation() can be explained pretty simple. You’re calling the actual function you defined your relation with. Yours for distributor probably looks somewhat like this:

public function distributors(){
    return $this->hasMany('Distributor');

So when calling $store->distributors() you just get the return value of $this->hasMany('Distributor') which is an instance of Illuminate\Database\Eloquent\Relations\HasMany

When do you use it?

You usually would call the relationship function if you want to further specify the query before you run it. For example add a where statement:

$distributors = $store->distributors()->where('priority', '>', 4)->get();

Of course you can also just do this: $store->distributors()->get() but that has the same result as $store->distributors.

Which brings me to the explanation of the dynamic relationship property.

Laravel does some things under the hood to allow you to directly access the results of a relationship as property. Like: $model->relation.

Here’s what happens in Illuminate\Database\Eloquent\Model

1) The properties don’t actually exist. So if you access $store->distributors the call will be proxied to __get()

2) This method then calls getAttribute with the property name getAttribute('distributors')

public function __get($key)
    return $this->getAttribute($key);

3) In getAttribute it checks if the relationship is already loaded (exists in relations). If not and if a relationship method exists it will load the relation (getRelationshipFromMethod)

public function getAttribute($key)
    // code omitted for brevity

    if (array_key_exists($key, $this->relations))
        return $this->relations[$key];

    $camelKey = camel_case($key);

    if (method_exists($this, $camelKey))
        return $this->getRelationshipFromMethod($key, $camelKey);

4) In the end Laravel calls getResults() on the relation which then results in a get() on the query builder instance. (And that gives the same result as $model->relation()->get().

How to append property with Accessors in Eloquent

Sometimes, we need to add more property to the Model or transform the original Model. We can define our own custom fields on top of existing in the database table. But, there is an Eloquent property name $appends to help us done this quickly.

I assume our User Model has these fields.

User Table

Since we only have one field to hold the full name. In this case, it names name. But in other contexts, we need to separate the full name down to the first name and the last name. Here $appends property comes to help.

We define the extent fields that we want to return them in $appends property of User Model.

class User extends Model
	protected $appends = [

Then, we define the related methods which return its value.

public function getFirstNameAttribute()

The name of the method adhere to this rule: get{camel_case_property_name}Attribute

For example: If our extent field name first_name so the name of method should be getFirstNameAttribute

Finally, we return the new value for new property in this method.

	public function getFirstNameAttribute()
		if ( $this->name === '' ) return '';

		$name = explode( ' ', $this->name );

		return $name[0];

Connection Refused while testing in Laravel

While you are running the test case in Laravel, you may catch this error as image shown below.

The error said your configuration was wrong. So, you need to check it. There is a difference a bit in the Testing environment is the Laravel App is using another configuration file. It used the file name  `.env.testing`. So you need to update that file same with .env file.

How to Set up Docker for Laravel 5

Okay, so today I have a task that I need to run a Laravel App. I decided to set up a Docker for it. So I could share the development environment for my teammate. At this point, I was thinking about a Docker image which helps me handle it. So I researched around and got some Docker Images. After I tried them, I felt they are challenging to use at least for me. So I decided to build a Docker Image to set up the environment for Laravel App. Here we go.

1. First Step – Prepare your Laravel resource

You should follow this article to know how to install Laravel 5. I used Laravel Installer. To check if it installed already by the command `which laravel`. If you got the message like this `.composer/vendor/bin/laravel`, congrats Laravel CLI ready to use. Create your app folder which you will build Docker environment. In my case is `docker-laravel-5` `mkdir docker-laravel-5` > Note: I used OSX Then cd to `docker-laravel-5` folder and type this line in terminal `laravel new app`. This command will pull the initial source and put them all inside the app folder.

2. Second Step – Prepare to Build Docker environment

In `docker-laravel-5` folder, you create a `docker-compose.yml` file. That file contains our Docker configuration. To find out more refer Docker Compose Documentation. `> docker-compose.yml` I also create a `build` and `config`, `data` folders to use later. > The current version of Docker Compose is 3.7. But in my app, I used version 3.0. We will need PHP and Mysql to run a Laravel App. We don’t need a full web server here. Laravel builds a Local Development Server. We only need to boot it. At this time, our `docker-compose.yml` looks like this
version: '3'


  // we will define out services here
We will define 2 services for PHP and Mysql soon.

2.1 PHP-FPM Service

Create a folder named `build`. That folder contains our own Dockerfile. Refer the Dockerfile Document. Then I created a file name `php.dockerfile` to config my PHP service.
FROM php:7.2-fpm

RUN apt-get update && apt-get install -y \
mysql-client libmagickwand-dev --no-install-recommends \
libfreetype6-dev \
libjpeg62-turbo-dev \
libpng-dev \
&& docker-php-ext-install -j$(nproc) iconv \
&& docker-php-ext-configure gd --with-freetype-dir=/usr/include/ --with-jpeg-dir=/usr/include/ \
&& docker-php-ext-install -j$(nproc) gd \
&& docker-php-ext-install pdo pdo_mysql

RUN php -r "copy('', 'composer-setup.php');" \
&& php -r "if (hash_file('SHA384', 'composer-setup.php') === '544e09ee996cdf60ece3804abc52599c22b1f40f4323403c44d44fdfdd586475ca9813a858088ffbc1f233e9b180f061') { echo 'Installer verified'; } else { echo 'Installer corrupt'; unlink('composer-setup.php'); } echo PHP_EOL;" \
&& php composer-setup.php \
&& php -r "unlink('composer-setup.php');" \
&& chmod +x composer.phar \
&& mv composer.phar /usr/local/bin/composer

WORKDIR /var/www/html

ENTRYPOINT ["php", "artisan", "serve", "--host=", "--port=8181"]

  Add this to `docker-compose.yml` file.
      context: ./build
      dockerfile: php.dockerfile
      - ./app:/var/www/html
      - ./config/php.ini:/usr/local/etc/php/php.ini
      - 8088:8181
      - mysql
line 3: Define context for build parameter is `build` folder. line 4: Specific name of Dockerfile. line 6: We mounted `app` folder in the host machine to `/var/www/html` folder in the container. line 7: We mounted `php.ini` config file to `/usr/local/etc/php/php.ini` line 8-9: We set the port to listen on `8088` on the host machine to map with `8181` the port in the container. line 9-10: I set PHP service depends on MySQL service which I define later.

2.2 Mysql Service

We will need Mysql to store Laravel App data. So we define Mysql Service in `docker-compose.yml` likes this
    image: mysql:latest
      - 3306:3306
      - ./data:/var/lib/mysql
      - MYSQL_USER=root
      - MYSQL_DATABASE=spark
      - MYSQL_PASSWORD=root
line 2: We used office image on Docker Store for MySQL. line 3-4: We set the port for Mysql Service is on `3306`. You can change it. line 5-6: We mounted `data` folder in the host machine to `/var/lib/mysql` folder in the container. line 7-11: We define Mysql environment variables for Mysql Image. Ok, that’s it. Now, all we need to do is combine them together.
version: '3'


      context: ./build
      dockerfile: php.dockerfile
      - ./app:/var/www/html
      # - ./config/php.ini:/usr/local/etc/php/php.ini
      - 8088:8181
      - mysql

    image: mysql:latest
      - 3306:3306
      - ./data:/var/lib/mysql
      - MYSQL_USER=root
      - MYSQL_DATABASE=spark
      - MYSQL_PASSWORD=root
Save and run `docker-compose up –build` to build Laravel App container. Then access `http://localhost:8088` to see your result. Hope this help. Thanks for reading!

How to find your fan page ID

Sometimes you use a third party which asks you input your fan page ID. However, you don’t know how to get it. Let me show you how. It is very easy.

Ok, so I will show you two approaches to get your fan page ID

1. You can get it inside about section of your fan page normally

Go to your fan page and click About in menu item on the left sidebar.

Then scroll down and see the Page ID line value

2. The second way is to use this website to get your fan page ID

Go to website end type your fan page URL in the text box. Press Find numeric ID button and boom! your fan page ID showed up.

Eventually, you use that ID for whatever you want to do. Hope this helps. Thanks for reading.

How to add domain to whitelist of Facebook fan page

To use some products of Facebook such as Customer Chat Plugin, you need to add your domain site into whitelist of your fan page. Through this article, I will show you how to do this. It just has a few steps.

Firstly, you should go to settings fan page.

Next, click on the Messenger platform menu on the left

Finally, scroll down to White-listed domains section. Enter your domain into the text box and save.

Note: your domain should activate SSL before added.

How to mount S3 Bucket on Ubuntu using S3FS

Today I will show you how I config to mount S3 Bucket as a local file system on Ubuntu 16.04.

What is an Amazon S3 bucket?

As a document, [Amazon S3 bucket] is storage for the Internet. It is designed to make web-scale computing easier for developers.

Create an Amazon S3 Bucket

First of all, you need register an AWS account to manage your buckets. Since you have the account, Amazon S3 provides APIs for you to create and manage buckets. By default, you can create 100 buckets limit. If you need additional buckets, you can increase your bucket limit by submitting a service limit increase. In each bucket, you can store any number of objects. See [create a bucket document]. Since you have a bucket, you can start mounting process.

Installation S3FS package

S3FS allows Linux and Mac OS X to mount an S3 bucket via FUSE.
Following S3FS guideline, we need to ensure all dependencies installed.

sudo apt-get install automake autotools-dev fuse g++ git libcurl4-gnutls-dev libfuse-dev libssl-dev libxml2-dev make pkg-config

Ensure libfuse2 and libfuse-dev packages are installed. Then compile from master via the following commands:

git clone
cd s3fs-fuse
make && make install

Verify Fuse and F3FS installed
– Fuse

root@dac22c9e3125:/# pkg-config --modversion fuse

– F3FS

root@dac22c9e3125:/# s3fs --version
Amazon Simple Storage Service File System V1.82(commit:ab89b4c) with OpenSSL
Copyright (C) 2010 Randy Rizun <>
License GPL2: GNU GPL version 2 <>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.

Then, enter your S3 identity and credential in a file /path/to/passwd and set owner-only permissions:

echo AccessKey:SecretKey > /path/to/passwd
chmod 600 /path/to/passwd

Mount an S3 bucket

Create a mount point directory for the S3 bucket and use the s3fs command to mount it.

1. Create a directory for mounting the S3 bucket

root@dac22c9e3125:/# mkdir /var/www/html/s3-bucket
root@dac22c9e3125:/# chmod -R 755 /var/www/html/s3-bucket

2. Create cache directory

root@dac22c9e3125:/# mkdir /tmp/cache 
root@dac22c9e3125:/# chmod -R 755 /tmp/cache

3. Mount the S3 bucket using the correct permissions and any other options

root@dac22c9e3125:/# s3fs -o allow_other -o use_cache=/tmp/cache mybucket /var/www/html/s3-bucket -o passwd_file=/.passwd-s3fs -o nonemprt

4. Verify the s3fs mounted file system

root@dac22c9e3125:/# df
s3fs on /var/www/html/s3-images type fuse.s3fs (rw,nosuid,nodev,relatime,user_id=0,group_id=0,allow_other)

5. You can also mount on boot

mybucket /path/to/mountpoint fuse.s3fs _netdev,allow_other 0 0


How to install PHP 7.0 or PHP 7.1 on Ubuntu

Most of the servers use PHP 5.5.9 or 5.6. Those PHP versions are not going to support soon. Since more and more applications use PHP 7.x to build, then we also need to upgrade it to our server.

Note: I am using Ubuntu 14.04. This way works for Ubuntu 16.04 as well.

Install PHP 7.0

First of all, you need to update modules

sudo apt-get update

Then, you can list all of the available PHP 7.0 packages for review

apt-cache pkgnames | grep php7.0

PHP 7.0 packages
PHP 7.0 packages

Select the packages that you want to install

sudo apt-get install php7.0 libapache2-mod-php7.0 php7.0-cli php7.0-common php7.0-mbstring php7.0-gd php7.0-intl php7.0-xml php7.0-mysql php7.0-mcrypt php7.0-zip

After the installation, check PHP version with command

php -v

The output should resemble

PHP 7.0.15-0ubuntu0.16.04.4 (cli) ( NTS )
Copyright (c) 1997-2017 The PHP Group
Zend Engine v3.0.0, Copyright (c) 1998-2017 Zend Technologies
with Zend OPcache v7.0.15-0ubuntu0.16.04.4, Copyright (c) 1999-2017, by Zend Technologies

Install PHP 7.1

First of all, you also need to check wether PHP 7.1 is available or not

apt-cache pkgnames | grep php7.1

If it is still not yet, so you need to download it.
You add specified PPA to the system apt repository

sudo apt-get install -y python-software-properties
sudo add-apt-repository -y ppa:ondrej/php

Note: If you get this error add-apt-repository command not found, you need to install this module:

sudo apt-get install software-properties-common

After that run update command again

sudo apt-get update -y

Finally, you can install PHP 7.1 same PHP 7.0. Just change 7.0 to 7.1

sudo apt-get install php7.1 libapache2-mod-php7.1 php7.1-cli php7.1-common php7.1-mbstring php7.1-gd php7.1-intl php7.1-xml php7.1-mysql php7.1-mcrypt php7.1-zip

Ok, now you need to disable PHP 5 module

sudo a2dismod php5.6

and active PHP 7.x module

sudo a2enmod php7.x

Restart your server to apply the changes

service apache2 restart

That’s it. Hope this help.

How to Fix WordPress Posts Returning 404 Not Found

Sometimes you may look at this URL on your browser
When you try to remove index.php out of URL, you get 404 Not Found page. Or for some reasons your site gets 404 Not Found for all links. So below are what I did when I got this situation.

Save your permalink again

First of all, you need to check your permalink in admin settings and save it again.
Go to Settings » Permalinks and just click on Save Changes button

Save your permalink
Save your permalink

This action will rewrite .htaccess file and store permalink structure into the database. In most case, this solution the WordPress 404 Error. If not, you have to check the .htaccess file manually. Using FTP or SSH it up to you and then open the .htaccess file (you might change permission to 666 temporary to edit it). Then add this code to .htaccess file

# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]

# END WordPress

If this bug still exists. So you need to use next solution to check.

Check Local Server Configuration

This bug causes your Apache does not active rewrite module. So you need to check this module activated or not yet.
Create a test PHP file and invoke phpinfo() function to look at our configuration.
If the module is activated, so it will be the list of Loaded Modules.

Check Local Server Configuration
Check Local Server Configuration

If not, you need to activate it. Open your terminal and type this command:

sudo a2enmod rewrite

And restart your server

sudo service apache2 restart

If still not working, so you need to check further a little bit.
Check your Apache config file. It located in /etc/apache2/apache2.conf
Open it in your editor. I like nano, so I use it to open this file

sudo nano /etc/apache2/apache2.conf

Find to this snippet config:

<Directory "path/to/your/document/root"> 
# ....

AllowOverride None

# ....


And change

AllowOverride None


AllowOverride All

After that restart your server again and check your site URL.
Hope this help!