Prerequisites:
- A basic knowledge of Laravel
- A basic knowledge of Rest APIs
- Have the laravel installer installed on your computer
- 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.
- 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:
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
//app/Http/Models/CameraModem.php
public function cameraCategory() { return $this->belongsTo(CameraCategory::class, 'camera_category_id');}
4.Allowing mass assignment on some fields