<?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');
}
}
<?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");
}
}
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`);
Hola Artyom Este Curso de React con laravel lo vas a terminar? Gracias por tus tutoriales me han ayudado bastante.
Hola Artyom espero que teencentres ben. una pregunta este mini curso de va acontinuar o solo eshasta aqui?
Continuar, perdon la demora. He estado muy ocupado.