crear migraciones para postslar11

Created at: 2024-08-27 18:20:23 | Updated at: 2024-08-27 18:25:01

1. Crear Migraciones

Para crear migraciones en Laravel, usa el comando php artisan make:migration seguido del nombre de la migración. Por ejemplo:

php artisan make:migration create_categories_table
php artisan make:migration create_etiquetas_table
php artisan make:migration create_posts_table
php artisan make:migration create_post_etiquetas_table
php artisan make:migration create_users_table

Cada uno de estos comandos generará un archivo de migración en el directorio database/migrations. Luego, debes editar estos archivos para definir la estructura de las tablas.

2. Definir Migraciones

Aquí está el contenido que debes colocar en cada archivo de migración:

create_categories_table.php

<?php

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

class CreateCategoriesTable extends Migration
{
    public function up()
    {
        Schema::create('categories', function (Blueprint $table) {
            $table->id(); // Autoincremental primary key
            $table->string('name');
            $table->text('svg');
            $table->timestamps(); // Automatically creates created_at and updated_at columns
        });
    }

    public function down()
    {
        Schema::dropIfExists('categories');
    }
}

create_etiquetas_table.php

<?php

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

class CreateEtiquetasTable extends Migration
{
    public function up()
    {
        Schema::create('etiquetas', function (Blueprint $table) {
            $table->id(); // Autoincremental primary key
            $table->string('name');
            $table->text('svg');
            $table->timestamps(); // Automatically creates created_at and updated_at columns
        });
    }

    public function down()
    {
        Schema::dropIfExists('etiquetas');
    }
}

create_posts_table.php

<?php

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

class CreatePostsTable extends Migration
{
    public function up()
    {
        Schema::create('posts', function (Blueprint $table) {
            $table->id(); // Autoincremental primary key
            $table->string('title');
            $table->foreignId('category_id')->nullable()->constrained('categories')->onDelete('set null');
            $table->string('excerpt', 500)->nullable();
            $table->text('content')->nullable();
            $table->string('img')->default('default_image.png');
            $table->string('url')->nullable();
            $table->string('ins')->nullable();
            $table->string('face')->nullable();
            $table->string('youtube')->nullable();
            $table->timestamps(); // Automatically creates created_at and updated_at columns
        });
    }

    public function down()
    {
        Schema::dropIfExists('posts');
    }
}

create_post_etiquetas_table.php

<?php

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

class CreatePostEtiquetasTable extends Migration
{
    public function up()
    {
        Schema::create('post_etiquetas', function (Blueprint $table) {
            $table->id(); // Autoincremental primary key
            $table->foreignId('post_id')->constrained('posts')->onDelete('cascade');
            $table->foreignId('etiqueta_id')->constrained('etiquetas')->onDelete('cascade');
            $table->timestamps(); // Automatically creates created_at and updated_at columns
        });
    }

    public function down()
    {
        Schema::dropIfExists('post_etiquetas');
    }
}

create_users_table.php

El archivo de migración para la tabla users es generalmente generado por Laravel cuando se crea una nueva aplicación y se ejecuta php artisan migrate. Aquí está cómo debería verse si necesitas ajustar algo:

<?php

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

class CreateUsersTable extends Migration
{
    public function up()
    {
        Schema::create('users', function (Blueprint $table) {
            $table->id(); // Autoincremental primary key
            $table->string('name');
            $table->string('email')->unique();
            $table->timestamp('email_verified_at')->nullable();
            $table->string('password');
            $table->text('two_factor_secret')->nullable();
            $table->text('two_factor_recovery_codes')->nullable();
            $table->string('remember_token')->nullable();
            $table->foreignId('current_team_id')->nullable();
            $table->string('profile_photo_path', 2048)->nullable();
            $table->timestamps(); // Automatically creates created_at and updated_at columns
        });
    }

    public function down()
    {
        Schema::dropIfExists('users');
    }
}

3. Ejecutar las Migraciones

Una vez que hayas definido todas las migraciones, ejecuta el siguiente comando para aplicar las migraciones y crear las tablas en tu base de datos:

php artisan migrate

Resumen

Estas migraciones permiten a Laravel gestionar las tablas de tu base de datos de manera estructurada y automática.

No valid image directory found or the category is not valid for the gallery.

Back to Posts
crear migraciones para postslar11

Title

Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.