Step By Step Laravel RESTful API

I am building a Laravel application using Laravel 5.6 in this tutorial. Let's start by creating a new Laravel project.
composer create-project laravel/laravel velreax "5.6.*"
or
composer create-project --prefer-dist laravel/laravel velreax
Open the velrex folder. open the composer.json file and add the following dependencies.
"tymon/jwt-auth": "0.5.*",
"barryvdh/laravel-cors": "^0.11.0"
then update the composer libraries
composer update
Open App.php in the config folder and add the following item into the providers array
Tymon\JWTAuth\Providers\JWTAuthServiceProvider::class,
Barryvdh\Cors\ServiceProvider::class
 and also add the aliases array with the following items
'JWTAuth' => Tymon\JWTAuth\Facades\JWTAuth::class,
'JWTFactory' => Tymon\JWTAuth\Facades\JWTFactory::class,

Prepare your database and setup your database configuration in your .env file

Edit your AppServiceProvider.php file (usually located in app/Providers folder) and inside the boot method set a default string length. We need to do this to handle Migration issue
[Illuminate\Database\QueryException]
SQLSTATE[42000]: Syntax error or access violation: 1071 Specified key was too long; max key length is 767 bytes 
use Illuminate\Support\Facades\Schema;
public function boot()
{
    Schema::defaultStringLength(191);
}
By default Laravel 5.6 comes with users table and password reset table migration files. Open the terminal or command prompt and run the command below
php artisan make:migration create_articles_table
This would create a file in the \database\migrations\ folder with the time stamp appended before the file name, e.g  “2018_04_23_094544_create_articles_table.php”.

Inside the file, add the following code block
<?php

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

class CreateArticlesTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('articles', function (Blueprint $table) {
            $table->increments('id');
            $table->integer('user_id')->unsigned();
            $table->foreign('user_id')->references('id')->on('users');
            $table->string('title');
            $table->string('slug')->unique();
            $table->text('excerpts');
            $table->text('body');
            $table->timestamps();
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::drop('articles');
    }
}
Run the command below in the terminal or command prompt
php artisan migrate
This would creating the tables inside the database.

Komentar