Pt:2 Creating API end points with Laravel.
Introduction
Hello, we are beginning a new series on building API endpoints with laravel (REStful api), REST, which stands for relational state transfer, which is used for interaction from a frontend by just send data to the backend or retriving data from the backend through different transfer protocol.
Requirements
The following are the requirements for this tutorial.
- Download php 7.2.1
- Download Composer.exe
- Download xampp or wamp for windows.
- Download postman.
Difficulty level
This tutorial is rated as intermediate.
Recap of the previous tutorial.
In the last installation for this series, we installed a new laravel application, we also setup the .env
file. We created the migrations and model for our database.
Overview of today's task.
In this series, we are going to be using a package called fzaninotto/Faker
. Faker is a PHP library that generates fake data for you, it comes by default with laravel.
Lets begin.
We are going to be seeding the database with faker
, faker produces random data for the database
with faker, generating data is so easy.
Creating our first seeder.
Creating our ArticlesTableSeeder
we need to generate it from the command prompt using the command below.
$ php artisan make:seeder ArticlesTableSeeder
find the seeder class in database/seeds
Open the file ArticlesTableSeeder.php and add the code below.
<?php
use Illuminate\Database\Seeder;
use Framework\Article;
class ArticlesTableSeeder extends Seeder
{
public function run()
{
// Let's truncate our existing records to start from scratch.
Article::truncate();
$faker = \Faker\Factory::create();
// And now, let's create a few articles in our database:
for ($i = 0; $i < 50; $i++) {
Article::create([
'title' => $faker->sentence,
'body' => $faker->paragraph,
]);
}
}
}
The particular code below produces 50 entry in the database using the Factory model.
for ($i = 0; $i < 50; $i++) {
Article::create([
'title' => $faker->sentence,
'body' => $faker->paragraph,
]);
next up we run the command below on the command prompt to produce the data in the database.
$ php artisan db:seed --class=ArticlesTableSeeder
We repeat the same procedure for creating our user seeds
$ php artisan make:seeder UsersTableSeeder
The code above generates the seeder class, which you can find at database/seeds
open the file UsersTableSeeder.php
and add the code below.
<?php
use Illuminate\Database\Seeder;
use Framework\User;
class UsersTableSeeder extends Seeder
{
public function run()
{
// Let's clear the users table first
User::truncate();
$faker = \Faker\Factory::create();
// Let's make sure everyone has the same password and
// let's hash it before the loop, or else our seeder
// will be too slow.
$password = Hash::make('toptal');
User::create([
'name' => 'Administrator',
'email' => '[email protected]',
'password' => $password,
]);
// And now let's generate a few dozen users for our app:
for ($i = 0; $i < 10; $i++) {
User::create([
'name' => $faker->name,
'email' => $faker->email,
'password' => $password,
]);
}
}
}
To add the data or rather run the seed, enter the command below on your command prompt.
$ php artisan db:seed --class=UsersTableSeeder
Note
You can simply run all the seed class by adding the code below in the DatabaseSeeder class
which you can find at database/seeds
$this->call(ArticlesTableSeeder::class);
$this->call(UsersTableSeeder::class);
you can simply run all seeds by using the command
$ php artisan db:seed
here are views of the database,
Conclusion
Now we have data in our database, in the next part, we are going create our first API service.
Curriculum
Posted on Utopian.io - Rewarding Open Source Contributors
Hey @sirfreeman, your contribution was rejected by the supervisor @espoem because he found out that it did not follow the Utopian rules.
Upvote this comment to help Utopian grow its power and help other Open Source contributions like this one. Do you want to chat? Join me on Discord.
thanks your information sheering, I just wanna try it
Thank you for the contribution. It has been approved.
You can contact us on Discord.
[utopian-moderator]
The contribution cannot be approved since the content is taken from another website.