Using the Generators package to set up an app

Generators is a popular Laravel package that automates quite a bit of file creation. In
addition to controllers and models, it can also generate views, migrations, seeds,
and more, all through a command-line interface.

Getting ready

For this recipe, we’ll be using the Laravel 4 Generators package maintained by Jeffrey Way
that was installed in the Downloading and installing packages recipe. We’ll also need a
properly configured MySQL database.

How to do it…

Follow these steps for this recipe:
1. Open the command line in the root of our app and, using the generator, create a
scaffold for our cities as follows:
php artisan generate:scaffold cities –fields=”city:string”
2. In the command line, create a scaffold for our superheroes as follows:
php artisan generate:scaffold superheroes –fields=”name:string,
city_id:integer:unsigned”
3. In our project, look in the app/database/seeds directory and find a file named
CitiesTableSeeder.php. Open it and add some data to the $cities array
as follows:
<?php
class CitiesTableSeeder extends Seeder {
public function run()
{
DB::table(‘cities’)->delete();
$cities = array(
array(

‘id’ => 1,
‘city’ => ‘New York’,
‘created_at’ => date(‘Y-m-d g:i:s’,
time())
),
array(
‘id’ => 2,
‘city’ => ‘Metropolis’,
‘created_at’ => date(‘Y-m-d g:i:s’,
time())
),
array(
‘id’ => 3,
‘city’ => ‘Gotham’,
‘created_at’ => date(‘Y-m-d g:i:s’,
time())

);
DB::table(‘cities’)->insert($cities);
}
}
4. In the app/database/seeds directory, open SuperheroesTableSeeder.php
and add some data to it:
<?php
class SuperheroesTableSeeder extends Seeder {
public function run()
{
DB::table(‘superheroes’)->delete();
$superheroes = array(
array(
‘name’ => ‘Spiderman’,
‘city_id’ => 1,
‘created_at’ => date(‘Y-m-d g:i:s’,
time())
),
array(
‘name’ => ‘Superman’,
‘city_id’ => 2,
‘created_at’ => date(‘Y-m-d g:i:s’,
time())
),
array(
‘name’ => ‘Batman’,
‘city_id’ => 3,
‘created_at’ => date(‘Y-m-d g:i:s’,
time())

),
array(
‘name’ => ‘The Thing’,
‘city_id’ => 1,
‘created_at’ => date(‘Y-m-d g:i:s’,
time())
)
);
DB::table(‘superheroes’)->insert($superheroes);
}
}

5. In the command line, run the migration then seed the database as follows:
php artisan migrate
php artisan db:seed
6. Open up a web browser and go to http://{your-server}/cities. We will see
our data as shown in the following screenshot:

7. Now, navigate to http://{your-server}/superheroes and we will see our data
as shown in the following screenshot:

How it works…

We begin by running the scaffold generator for our cities and superheroes tables. Using the
–fields tag, we can determine which columns we want in our table and also set options
such as data type. For our cities table, we’ll only need the name of the city. For our superheroes
table, we’ll want the name of the hero as well as the ID of the city where they live.
When we run the generator, many files will automatically be created for us. For example, with
cities, we’ll get City.php in our models, CitiesController.php in controllers, and a
cities directory in our views with the index, show, create, and edit views. We then get a
migration named Create_cities_table.php, a CitiesTableSeeder.php seed file,
and CitiesTest.php in our tests directory. We’ll also have our DatabaseSeeder.php
file and our routes.php file updated to include everything we need.
To add some data to our tables, we opened the CitiesTableSeeder.php file and updated
our $cities array with arrays that represent each row we want to add. We did the same
thing for our SuperheroesTableSeeder.php file. Finally, we run the migrations and seeder
and our database will be created and all the data will be inserted.
The Generators package has already created the views and controllers we need to
manipulate the data, so we can easily go to our browser and see all of our data. We can also
create new rows, update existing rows, and delete rows.

Leave a Reply

Your email address will not be published. Required fields are marked *