Most APIs need to authenticate their users so we'll start with user accounts. In this section I will show you how to create basic Account functionality.
We're goint to user Laravel's Migrations feature to automate the creation of the MySQL table to house accounts data.
php artisan make:migration create_accounts_table --fields="email:string, password:string, type:integer, firstName:string, lastName:string, facebookUserId:string, linkedinUserId:string, platform:string, dateCreated:date, dateUpdated:date"
This creates the folllowing file:
app/database/migrations/2014_08_29_215145_create_accounts_table.php
You can remove this table with the command:
php artisan generate:migration delete_accounts_table
If you make a mistake and need to remove a column:
php artisan generate:migration remove_datecreated_from_accounts_table --fields="dateCreated:date"
You can also add/remove/change columns by editing the migrations PHP file.
Migrations are an excellent way to build up your database. They provide, in code, the full history of your database changes which means you'll always be able to be sure that you're deploying the correct version of your database schema.
You can use Laraval's seed feature to add sample data to your application. Again we use the generator to do this:
This command uses the "faker" project, so we first need to add it to our dependencies in composer.json:
-
Add to require-dev:
"fzaninotto/faker": "v1.4.0"
-
composer update --dev
-
php artisan make:seed accounts
This will generate the seed template that you can fill in with data.
-
Edit the file
app/database/seeds/AccountsTableSeeder.php
and make it look like this:<?php // Composer: "fzaninotto/faker": "v1.4.0" use Faker\Factory as Faker; class AccountsTableSeeder extends Seeder { public function run() { $faker = Faker::create(); foreach(range(1, 10) as $index) { Account::create([ 'email' => 'testuser' . $index . '@example.com', 'firstName' => 'User', 'lastName' => $index, 'password' => 'password' ]); } } }
-
The last step is to add this seeder class to the
app/database/seeds/DatabaseSeeder.php
file:<?php class DatabaseSeeder extends Seeder { /** * Run the database seeds. * * @return void */ public function run() { Eloquent::unguard(); $this->call('AccountsTableSeeder'); } }
Now we have the database table in our migrations, we can create a basic, empty model.
php artisan generate:model Account
Now that we've set up a table, some seed data, and a model to store it in we need to test it:
- Create the Accounts table:
php artisan migrate
- Add the seed data:
php artisan db:seed
First we'll scaffold out a Controller:
php artisan generate:controller Account
We will look at the Controller in more detail later.
php artisan generate:view account.index
There is a shortcut to the above. Using generate:resource, we can create all the components we need for the endpoint. This is the easiest way to create everything, but you can use the individual commands when you need to change individual components.
php artisan generate:scaffold account --fields="email:string, password:string, type:integer, firstName:string, lastName:string, facebookUserId:string, linkedinUserId:string, platform:string"
Note: I left out dateCreated and dateUpdated because Laravel model's include this by default.
You can choose to exclude some components if you've already created them.
The above scaffold command will give you the route code you need to add to app/routes.php
This will be:
Route::resource('account', 'AccountsController');
If you haven't already, do these steps:
- Create the Accounts database table:
php artisan migrate
- Add the seed data:
php artisan db:seed
Then:
-
Open your browser and navigate to http://laravel-api.phplocal.dev/account/1
-
At the moment the endpoint doesn't do anything, it just shows a view. So you'll just see the path to the view:
/home/vagrant/projects/laravel-api2/app/views/accounts/show.blade.php
In the next steps, we'll configure the Account endpoint to function as required.