Curso ReactJS y Laravel Directorio de Empresas
Modelos: User, Categoria, Empresa y relaciones
Implementación de Modelos como User, Categoria y Empresa además de las relaciones de integridad referencial. Migración para obtener las tablas en MYSQL
Modelos
public function up(): void
{
Schema::create('users', function (Blueprint $table) {
$table->id();
$table->string('name');
$table->string('email')->unique();
$table->timestamp('email_verified_at')->nullable();
$table->string('password');
$table->rememberToken();
$table->timestamps();
});
}
protected $fillable = [
'name',
'email',
'password',
];
public function up(): void
{
Schema::create('categorias', function (Blueprint $table) {
$table->id();
$table->string("nombre",25);
$table->string("slug",25)->unique();
$table->text("descripcion")->nullable();
$table->boolean("menu")->default(0);
$table->integer("orden")->default(1);
});
}
class Categoria extends Model
{
use HasFactory;
protected $guarded = [];
public $timestamps = false;
}
public function up(): void
{
Schema::create('empresas', function (Blueprint $table) {
$table->id();
$table->string("nombre",50);
$table->string("email",50)->unique();
$table->string("telefono",9);
$table->string("direccion",50);
$table->string("website",50)->nullable();
$table->string("facebook",50)->nullable();
$table->string("youtube",50)->nullable();
$table->string("tiktok",50)->nullable();
$table->text("descripcion")->nullable();
$table->string("urlfoto",50)->nullable();
$table->boolean("publicado")->default(0);
$table->integer("orden")->default(1);
$table->integer("visitas")->default(1);
$table->foreignId('categoria_id')->references('id')->on('categorias')->onUpdate('cascade')->onDelete('cascade');
$table->foreignId('user_id')->references('id')->on('users')->onUpdate('cascade')->onDelete('cascade');
$table->timestamps();
});
}
class Empresa extends Model
{
use HasFactory;
protected $guarded = [];
}
class Empresa extends Model
{
use HasFactory;
protected $guarded = [];
public function categoria()
{
return $this->belongsTo(Categoria::class);
}
public function user()
{
return $this->belongsTo(User::class);
}
}
class User extends Authenticatable
{
use HasApiTokens, HasFactory, Notifiable;
....
public function empresas()
{
return $this->hasMany(Empresa::class);
}
}
class Categoria extends Model
{
use HasFactory;
protected $guarded = [];
public $timestamps = false;
public function empresas()
{
return $this->hasMany(Empresa::class);
}
}
DB_DATABASE=dbdiremp
DB_USERNAME=root
DB_PASSWORD=
php artisan migrate
486 visitas
« Capítulo 3 – Instalación de Laravel y React JS
Capítulo 5 – Rutas y Controladores »
Descarga el código fuente del proyecto adquiriendo el curso completo
Comprar© Copyright Codea App | LATAM | 2020 - 2024