Search⌘ K
AI Features

Launching Restaurants APIs

Explore how to build and secure API endpoints for managing restaurants using Laravel. Learn to create migrations and Eloquent models with soft deletes, enforce data validation, and protect routes with Sanctum authentication. By the end of this lesson, you will understand how to set up the foundation for fast and secure restaurant APIs.

Migration and Eloquent model

Each new module starts with a database table. Here as well, we will first create the restaurants table first. Let’s go through the migration file first:

PHP
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
class CreateRestaurantsTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('restaurants', function (Blueprint $table) {
$table->id();
$table->foreignId('user_id')->constrained();
$table->string('name');
$table->string('address_line_1');
$table->string('address_line_2')->nullable();
$table->string('pincode');
$table->string('city');
$table->foreignId('state_id')->nullable()->constrained();
$table->foreignId('country_id')->nullable()->constrained();
$table->timestamps();
$table->softDeletes();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('restaurants');
}
}

The columns are self-explanatory. We have only kept a few columns compulsory. We are also going to use the ‘Soft Delete’ feature to archive the records instead of deleting ...