...

/

Launching Restaurants APIs

Launching Restaurants APIs

Learn how to set up the building blocks of the new module in a way that facilitates easy maintenance.

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:

Press + to interact
<?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 ...