ReactJS Hooks + Laravel 8 + 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 Employee
php artisan make:model Role
Role
app/Models/Role.php
<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

class Role extends Model
{
    use HasFactory;

    protected $table = "role";

    protected  $primaryKey = "rol_id";

    protected $fillable = [
      'rol_name'
    ];

    public $timestamps = false;
}


Employee
app/Models/Employee.php
<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

class Employee extends Model
{
    use HasFactory;

    protected $table = "employee";

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

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

Test

php artisan make:controller API/EmployeeController 
Controller API
app/Http/Controllers/API/EmployeeController.php
<?php

namespace App\Http\Controllers\API;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use App\Models\Role;

class EmployeeController extends Controller
{
    //
    public function list_role(){ 
      $data = Role::get();

      $response['data'] = $data;
      $response['succes'] = true;
      return $response;
    }
}

Route
routes/api.php
Route::get('/employee/role', 'App\Http\Controllers\API\[email protected]_role');
SQL

CREATE TABLE `role` (
  `rol_id` bigint(20) UNSIGNED NOT NULL,
  `rol_name` varchar(255) COLLATE utf8mb4_unicode_ci NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

--
-- Volcado de datos para la tabla `role`
--

INSERT INTO `role` (`rol_id`, `rol_name`) VALUES
(1, 'Admin'),
(2, 'Software engineer'),
(3, 'Tester');

--
-- Índices para tablas volcadas
--

--
-- Indices de la tabla `role`
--
ALTER TABLE `role`
  ADD PRIMARY KEY (`rol_id`);

5 comentarios sobre “ReactJS Hooks + Laravel 8 + MySQL + API REST CRUD #2 Migration & Models”

Deja una respuesta

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