Hello powerfullhoster! Today i'll show you how to make API using Laravel!. This is part1 and we'll continue with another part in another day! Let's check this out!.
Note: This article assumes the reader already understands web development with laravel groove. If the head feels dizzy and confused was reading what, you should read first wrote to completion, guns have practiced. Check the link at the end the writing reading material.
Make API, has now become one of the skill required to create a web developer. How not, from smart phones to light the house (read: IOT) now "need" API to be more "intelligent". Many programming languages that can be used to make the API. But, since I am a Web Developer with a skillset in laravel, let's make this API with laravel framework.
In this tutorial, we will build an API for the data invokes hotel. Let us call the application that we are building as Travelia. This tutorial will be made radiant. In this first part, we will prepare a project and a little theory about the API. Happy reading!
Project Preparation
Create a new laravel first application with the command:
composer create-project laravel/laravel travelia
At the time of this tutorial is written, I use laravel version 5.2.39. Please equate upgraded by adding laravel / laravel = 5.2.39 if worried experienced an error due to different versions.
Once ready, please configuration database. To facilitate the process of development, we will run laravel with:
php artisan serve
In Travelia,
hotels table structure will be as follows:
To create this table, we will use the migration. Let us create a model hotel migrationnya with the following command:
php artisan make:model Hotel -m
In the resulting migration, fill
up with the method:
public function up()
{
Schema::create('hotels', function (Blueprint $table) {
$table->increments('id');
$table->string('name');
$table->string('city');
$table->string('address');
$table->string('country');
$table->string('phone');
$table->string('email');
$table->string('website');
$table->decimal('latitude', 16, 6);
$table->decimal('longitude', 15, 6);
$table->timestamps();
});
}
We enable
mass assignment for each field that is created on the model Hotel:
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Hotel extends Model
{
protected $fillable = ['name', 'city', 'address', 'country', 'phone',
'email', 'website', 'latitude', 'longitude'];
}
Run the migration with the command:
php artisan migrate
Make sure the table has appeared in a database of hotels.
Data Sample
In developing the API, even web development in general, the use of sample data will greatly simplify the application development process. For the development of the API, sample data will help us in checking the appropriate response to the API. In laravel, to create sample data we can use the features of seeder. Let us create a table seeder for hotels with the command:
php artisan make:seeder HotelsSeeder
In the generated file, we fill it with the following syntax:
<?php
use Illuminate\Database\Seeder;
use App\Hotel;
class HotelsSeeder extends Seeder
{
public function run()
{
$faker = Faker\Factory::create();
foreach (range(1, 100) as $i) {
$website = $faker->domainName;
Hotel::create([
'name' => $faker->streetName . ' Hotel',
'city' => $faker->city,
'address' => $faker->streetAddress,
'country' => $faker->country,
'phone' => $faker->phoneNumber,
'email' => $faker->userName . '@' . $website,
'website' => $website,
'latitude' => $faker->latitude,
'longitude' => $faker->longitude
]);
}
}
}
In the generated file, we fill it with the following syntax: In the syntax above, we use Faker to make 100 hotel data randomly. To facilitate seeding, let's call this seeder from the
databasefile/seeds/DatabaseSeeder.php:
<?php
use Illuminate\Database\Seeder;
class DatabaseSeeder extends Seeder
{
public function run()
{
$this->call(HotelsSeeder::class);
}
}
Run the following command in order to sample this data appears in the database:
php artisan db:seed
Make sure the table has been filled with 100 hotels record.
Understanding Endpoint
In making the API, the endpoint is the term used to mention the URL on the API being built. For example we have a URL
https://travelia.com/auth/token we could call endpoint /auth/token
Belum ada tanggapan untuk "Make API Using Laravel #Part1"
Post a Comment