ReactJS Hooks + Laravel 7 + MySQL + API REST CRUD #2 Migration & Models

Laravel

Migrations

php artisan make:migration create_role_table
php artisan make:migration create_employee_table
Role
database/migrations/2020_08_09_162202_create_role_table.php
<?php

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

class CreateRoleTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('role', function (Blueprint $table) {
            $table->id("rol_id");
            $table->string('rol_name');
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::dropIfExists('role');
    }
}

Employee
database/migrations/2020_08_09_162205_create_employee_table.php
<?php

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

class CreateEmployeeTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('employee', function (Blueprint $table) {
            $table->id();
            $table->string('name_lastname');
            $table->string('email');
            $table->string('city');
            $table->string('direction');
            $table->bigInteger('phone');
            $table->unsignedBigInteger('rol');
            $table->foreign('rol')->references('rol_id')->on('role');
            $table->timestamps();
        });

    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::dropIfExists('employee');
    }
}

Models

php artisan make:model Models/Employee
php artisan make:model Models/Role
Role
app/Models/Role.php
<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;

class Employee extends Model
{

    protected $table = "employee";

    protected $fillable = [
      'name_lastname',
      'email',
      'city',
      'direction',
      'phone',
      'rol'
    ];

    public function role(){ 
      return $this->belongsTo('App\Models\Role','rol');
    }
}

Employee
app/Models/Employee.php
<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;

class Employee extends Model
{

    protected $table = "employee";

    protected $fillable = [
      'name_lastname',
      'email',
      'city',
      'direction',
      'phone',
      'rol'
    ];

    public function role(){ 
      return $this->belongsTo('App\Models\Role','rol');
    }
}

Deja una respuesta

Tu dirección de correo electrónico no será publicada. Los campos obligatorios están marcados con *