Nov 032013
 

Introduction

One of the things that takes up a precious amount of time when developing Laravel applications is creating sample data. Fortunately, Laravel provides a useful command within Artisan for seeding data.

However, this routine requires that you supply an array of associative arrays containing the data and when you want to seed a table with more than a dozen or so records, this can get quite code intensive.

Enter FillText Data Service

Having done a lot of AngularJS development, I have used a wonderful data service called FillText which provides a REST API allowing your create requests that give you a wide variety of commonly used data fields (names, addresses, dates, etc).

For example, you could create a call such as http://www.FillText.com/?rows=5&fname={firstName} which will return 5 rows of JSON data including random firstName fields. While this is just a small example of what can be done using FillText, you can be very creative and create calls to return just about any type of data (visit the FillText website for more information).

Configure Laravel

In order to use FillText in your seeding class, you will need to install a cURL package so that you can before the API call to the FillText server. I have chosen anlutro/curl as it is ready for Laravel 4 and it was simply did the trick (there are others in packagist registry, but they were not updated to support Laravel 4 packages)

1. Add “anlutro/curl”: “dev-master” to your composer.json file


"require": {
  ...
  "anlutro/curl": "dev-master"
}

2. Execute Composer Update to install the package

3. Update your app.php file to include the service provider and alias


Service Provider:

'providers' => array(
   ...
   'Illuminate\Workbench\WorkbenchServiceProvider',
   'anlutro\cURL\Laravel\cURLServiceProvider',
)

Class Aliases:

'aliases' => array(
   ....
   'cURL'        => 'anlutro\cURL\Laravel\cURL'  
)

4. Create your new seeder file using Artisan (this example uses Jeffrey Way / Generators)


$ php artisan generate:seed Contacts

5. Open the new seed file in your editor and you can now build your FillText call to suit your needs. The following example I am using will create 100 rows of random fname, lname, and phone properties as well as random created_at and updated_at values (using FillText date range features).


<?php

use anlutro\cURL\cURL;

class ContactsTableSeeder extends Seeder {

  public function run()
  {
	// Uncomment the below to wipe the table clean before populating
	DB::table('contacts')->truncate();

	$curl = new cURL;
	$url = 'http://www.filltext.com/?rows=100&fname={firstName}&lname={lastName}&phone={phone|format}&created_at={date|01-01-2013,11-30-2013}&updated_at={date|01-01-2013,11-30-2013}';
	$data = $curl->get($url);
	$contacts = json_decode($data, true);

	// Uncomment the below to run the seeder
	DB::table('contacts')->insert($contacts);
   }

}


Conclusion

As you can see, the process for building a FillText call is quite simple and there are a myriad of possibilities as to what can be done to create sample data. Take some time to peruse the FillText website and play around with the different options and settings.

Joe Maddalone (author of FillText) has posted a new video showing the latest features on his YouTube Channel. You can check it out here FillText.com updates November 2013

Sep 102013
 

Introduction

When initially installing Laravel 4 framework, the default database is set to use MySQL. While MySQL may be fine for most users and applications, we prefer to use the advanced features of PostgreSQL.

This post will outline the steps necessary to use PostgreSQL with Laravel 4. Switching between PostgreSQL and MySQL is really as simple as making ONE change in the database.php file (define the default database) outlined in Step 1. The rest of this post will guide you step by step into activating PostgreSQL and testing to see new data.

Note: This post does not outline the actual installation and configuration of PostgreSQL. There are several tutorials on the internet which cover this topic quite nicely so we have focused on just the Laravel 4 changes.

Configure Laravel

First up, we need to configure the /app/config/database.php file

1. Set default property


/*
|--------------------------------------------------------------------------
| Default Database Connection Name
|--------------------------------------------------------------------------
|
| Here you may specify which of the database connections below you wish
| to use as your default connection for all database work. Of course
| you may use many connections at once using the Database library.
|
*/

'default' => 'pgsql',

2. Set the ‘pgsql’ property in ‘connections’ array


		'pgsql' => array(
			'driver'   => 'pgsql',
			'host'     => 'localhost',
			'database' => 'postgres-test',
			'username' => 'mikee',
			'password' => '',
			'charset'  => 'utf8',
			'prefix'   => '',
			'schema'   => 'public',
		),

3. Create a new migration (I used Jeffrey Way Generator for this purpose, creating a full scaffold for entire application)

LINK: You can download Jeffrey Way Laravel Generator Here — Laravel Generator


<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;

class CreateCustomersTable extends Migration {

	/**
	 * Run the migrations.
	 *
	 * @return void
	 */
	public function up()
	{
		Schema::create('customers', function(Blueprint $table) {
			$table->increments('id');
			$table->string('customer_name');
			$table->string('address');
			$table->string('city');
			$table->string('state');
			$table->string('zip');
			$table->string('county');
			$table->timestamps();
		});
	}

	/**
	 * Reverse the migrations.
	 *
	 * @return void
	 */
	public function down()
	{
		Schema::drop('customers');
	}

}

4. Create some seed data (this can really be anything you wish, just put some data into the new Customers table we created in Step 3.


<?php

class CustomersTableSeeder extends Seeder {

	public function run()
	{
		DB::table('customers')->truncate();

		$customers = array(
			['customer_name' => 'CodeDungeon', 'address' => '6372 Flint Drive', 'city' => 'Huntington Beach', 'state' => 'ca', 'zip' => '92647', 'created_at' => new DateTime, 'updated_at' => new DateTime],
			['customer_name' => 'ASG', 'address' => '16742 Gothard #2010', 'city' => 'Huntington Beach', 'state' => 'ca', 'zip' => '92647', 'created_at' => new DateTime, 'updated_at' => new DateTime],
			['customer_name' => 'ABC Company', 'address' => '1234 Main Street', 'city' => 'Huntington Beach', 'state' => 'ca', 'zip' => '92647', 'created_at' => new DateTime, 'updated_at' => new DateTime]
		);

		DB::table('customers')->insert($customers);
	}

}

5. Create Route Entry: If you have used the scaffold command from Generator, then the route has already been created for you. If not, you can do something like the following.


/*
|--------------------------------------------------------------------------
| Application Routes
|--------------------------------------------------------------------------
|
| Here is where you can register all of the routes for an application.
| It's a breeze. Simply tell Laravel the URIs it should respond to
| and give it the Closure to execute when that URI is requested.
|
*/

Route::get('/', function()
{
	// return View::make('hello');
});

Route::resource('customers', 'CustomersController');

6. Create Controller Entry: If you have used the scaffold command from Generator, then the entire controller method has already been created for you. If not, you can do something like the following.


	public function index()
	{
		$customers = $this->customer->all();

		return View::make('customers.index', compact('customers'));
	}