Wednesday, September 18, 2019

How to build a REST API in laravel 5.7

  1. Prerequisites:

    1. A basic knowledge of Laravel
    2. A basic knowledge of Rest APIs
    3. Have the laravel installer installed on your computer
     
  2.  What we'll be building:
    • The purpose of this demonstration, we'll be building a camera reviews API.
    • Users will be able to add create camera, update camera and delete camera.
    • Users will also be able to view a list all cameras and relationship in camera.
    • Finally, we'll add authentication with JSON Web Tokens( JWT) to make the API secured.
  3. Create a new Laravel app
  •  Via laravel installer 
                 Download the laravel installer using composer
             composer global require laravel/installer
  • Via Composer create project
          composer create-project --prefer-dist laravel/laravel api.camera
  • Local development server
      php artisan serve 
    
4.Create models and migrations
     A camera reviewers API will have 3 three models:
users,camera setting types, camera command types, camera settings. (camera category has many camera modems.)
We'll start by creating the camera category both controller and model:
> php artisan make:controller CameraCategoriesController --resource --model=CameraCategory
We'll do the same for the camera modem:
 > php artisan make:controller CameraModemsController --resource --model=CameraModem
  • Next, let's open the migration file generated for the camera category migrate and camera modem migrate. Update the up() method as below:
    public function up()
    {
        Schema::create('camera_categories', function (Blueprint $table){
          $table->increments('id');      $table->string('type_name');      $table->string('description');      $table->boolean('is_deleted')->default(0);      $table->boolean('is_published')->default(1);
        });}

public function up()
{
    Schema::create('camera_modems', function (Blueprint $table){
      $table->increments('id');      $table->string('modem_name');      $table->text('description')->nullable();      $table->text('MJPEG_stream_source')->nullable();      $table->text('H264_stream_source')->nullable();      $table->text('snapshot_source')->nullable();      $table->unsignedInteger('setting_type_id')->nullable();      $table->unsignedInteger('camera_category_id');      $table->foreign('camera_category_id')->references('id')
            ->on('camera_categories')->onDelete('cascade');    });}
public function down()
{
    Schema::dropIfExists('camera_modems',function (Blueprint $table) {
        $table->dropForeign('camera_category_id');    });}

we definne the fields for the camera modems table which are an auto increment ID.
Run the command below to run the migrations
> php artisan migrate

Remember to enter your database details in the .env file before running the command ablove.

Define relationships between models:
  • A user can management many camera categories, but a camera categories can only belong to one user. So, the relationship between the User model and camera categories modem is one-to-many relationship.
//app/Http/Models/Users.php
public function camera_categories(){
    return $this->hasMany(CameraCategory::class);
}  
    •       Next, let's define the inverse relation on the camera category model:

public function user(){
    return $this->belongTo(User::class);} 
 Likewise, a category can be have many camera modems. This is also a one-to-many relationship.Add the code below in the category model:

public function cameraModems(){
    return $this->hasMany(CameraModem::class);}
Then we define the inverse relationship inside the camera modem model:
 //app/Http/Models/CameraModem.php
public function cameraCategory()
{
    return $this->belongsTo(CameraCategory::class, 'camera_category_id');} 

4.Allowing mass assignment on some fields

 


               
 
 

No comments:

Post a Comment