# 2.- Creando nuestro primer model (Article)

  php artisan make:model Article -m
  • Se crearan 2 archivos el primero estara en app/Models y el segundo en app/database/migrations

  • Ingresamos al archivo app/database/migrations y agregamos los nuevos datos

 public function up()
    {
        Schema::create('articles', function (Blueprint $table) {
            $table->id();
            $table->string('title');
            $table->string('slug');
            $table->text('body');
            $table->foreignId('author_id')->constrained('users'); //Capturamos el id del user porque un users va a tener varios articulos
            $table->timestamps();
        });
    }

  • Ingresamos al archivo app/Models/User.php y agregamos los nuevos datos
  class Article extends Model
  {
      use HasFactory;

      protected $fillable = [
          'title',
          'slug',
          'body',
          'author_id',
      ];
  }

# Relacionamos la table Article con la tabla user

  • Nos dirigimos al archivo app/Models/User.php
    use App\Models\Articles;

    public function articles(){
        return $this->hashMany(Articles::class, 'author_id');
    }
  • Nos dirigimos al archivo app/Models/Articles.php
    use App\Models\User;

      public function user(){
        return $this->belongsTo(User::class);
    }