Laravel 4 and database seeding

I’ve been playing around with Laravel 4 for a few days now and have been very impressed. Once I figured outhow to install it, so far the learning curve from Codeigniter seems to be small. Right now I’m taking an existing project written with Codeigniter and creating a Laravel version for it. I got the configurations set up, now to move on to the database. The migration feature is great. I’ve been able to create my database and rollback with ease. The next step is to take data from an existing database and import it over. I could dump it into an SQL file and be done with it. But the source project is live and data changes. I’d like to use migration to get the latest data off the source.

The solution is to use Laravel’s seeding. Migration and seeding work with each other. Unfortunately, the sample code and demo I’ve seen so far uses arrays to populate (seed) the database. It’s a good sign though because all I have to do is query the existing database and populate an array with the resultset. The problem is how and what the syntax is. After a few hours of researching online and testing, I’ve got the solution.

Let’s say your source database is MySQL and you have a client table with client_id and client_name columns. On your Laravel project you have a clients table with id, client_name, created_at, and updated_at columns. Add a new connection to your /app/config/database.php. If you have multiple MySQL databases, it’s okay, just append 2… so you have a mysql and mysql2 connection. So your mysql array connection holds your Laravel database and mysql2 holds your database connection information where you’ll be getting data from.

Next edit the /app/database/seeds/DatabaseSeeder.php file. Here’s what I have.

class DatabaseSeeder extends Seeder {

	 * Run the database seeds.
	 * @return void
	public function run()



class ClientTableSeeder extends Seeder

	public function run()
                // truncate the table before inserting imported data

                // use mysql2 connection then query the database, then put resultset in array
		$clients = DB::connection('mysql2')->select('SELECT `client_id`, `client_name` FROM `client`');
		$now = date('Y-m-d H:i:s');

                // loop through the resultset and insert into laravel database
		foreach ($clients as $client)
				'id'			=> $client->client_id,
				'client_name' 	=> $client->client_name,
			    'created_at' 	=> $now,
			    'updated_at' 	=> $now

Setting up Laravel 4 on Linux Mint 16

I’ve been curious about Laravel and wanted to set up a virtual machine to get an environment for it. I will go over what I used and how I managed to finally get the environment working. Here is a list of what I will be using. I am including the version numbers just to add more detail. These are the versions I used at the time of writing this post. Terminal will be used to type the command and is assumed you are at your home directory.

I will be installing LAMP on Linux Mint by using

sudo apt-get install lamp-server^

This will install the following.

  • Apache 2.4.6
  • PHP 5.5.3
  • MySQL 5.5.35

You can install phpmyadmin if you want but those are the 3 main components. Next, head on over to the quick start guide You might ask, why I’m posting this when Laravel already has a tutuorial. Well, I ran into many issues and I wanted to take note of them and the solutions I found. So I’ll be referring to their guide.

First thing we’ll need to do is install composer. To install composer, you will use curl. If curl is not installed, then use the following command.

sudo apt-get install curl

You will also need json extension for PHP.

sudo apt-get install php5-json

If you have curl installed, then download composer with this command.

curl -s | php

Once download is completed, it will tell you where it downloaded and how to use it. We will move and rename composer.phar to composer and move it to /usr/local/bin.

sudo mv composer.phar /usr/local/bin/composer

You can check to see if it’s working by typing the following.

composer about

It should give you the description of composer. Now that you have composer installed, we can install Laravel. According to their guide, you need to download it from We’re going to use terminal to download it, then just like composer, we’re going to move it to /usr/local/bin.

sudo mv laravel.phar /usr/local/bin/laravel

Update: 2015-04-22: The file laravel.phar is no longer available. Essentially, you don’t need it. Composer is all you need. Thanks to @rohantapiyawala for giving me a heads up.

I will be storing my projects in the ~/Sites folder. For this tutorial, I will call the project apple. So this will sit in ~/Sites/apple.

mkdir ~/Sites
cd ~/Sites
composer create-project laravel/laravel apple --prefer-dist

You may get an error regarding the mcrypt PHP extension. The error looks like this.


Install the extension.

sudo apt-get install php5-mcrypt

Check if it is enabled.

php --ri mcrypt

If it’s not enabled/present then do the following.

sudo ln -s /etc/php5/conf.d/mcrypt.ini /etc/php5/mods-available
sudo php5enmod mcrypt
sudo service apache2 restart

Delete the apple project and use composer to create it. This time you shouldn’t get the mcrypt error. I found the solution at

rm -rf ~/Sites/apple
composer create-project laravel/laravel apple --prefer-dist

If you look inside the ~/Sites/apple directory, you will see public. This is the “public_html” of the project. Just set Apache virtualhost for that folder and you’re done. My file is located at /etc/apache2/sites-available/apple.conf and it looks like this…

	<Directory /home/sherwin/Sites/apple>
	Options Indexes FollowSymLinks
	AllowOverride All
	Require all granted
	ServerName apple.local
	ServerAdmin webmaster@localhost
	DocumentRoot /home/sherwin/Sites/apple/public
	ErrorLog /home/sherwin/Sites/apple/error.log
	CustomLog /home/sherwin/Sites/apple/access.log combined

Next you need to enable the new site and restart Apache.

# enable new site conf
sudo a2ensite apple.conf

# restart apache
sudo service apache2 restart

If you get a blank screen, you may need to give app/storage different permissions. The article at explains more.

# Group Writable (Group, User Writable)
sudo chmod -R gu+w app/storage
# World-writable (Group, User, Other Writable)
sudo chmod -R guo+w app/storage

You should now be able to load http://apple.local. If it works, you will get the Laravel logo and “You have arrived.” text. Coming from Codeigniter, I felt there is a lot more tasks just to get Laravel to work. It somewhat discourages me to continue experimenting but I’ll give it another chance. It’s my curiosity that drives it.

Getting started with Laravel

I’ve been playing around with other frameworks and Laravel caught my eye. But every time I try to install it, I get an error.

Mcrypt php5 is required. Script php artisan clear-compiled handling the post-install-cmd event returned with an error

I know mcrypt for php is installed but I guess it’s not enabled. I found the solution at Now I can continue to do the tutorial on