Herramientas de usuario

Herramientas del sitio


proyectos:registro_comidas

Diferencias

Muestra las diferencias entre dos versiones de la página.

Enlace a la vista de comparación

Ambos lados, revisión anteriorRevisión previa
Próxima revisión
Revisión previa
proyectos:registro_comidas [2025/03/30 17:19] – [Comidas] tempwinproyectos:registro_comidas [2025/03/30 18:54] (actual) – [Migración] tempwin
Línea 13: Línea 13:
  
 ===== Estructura de base de datos ===== ===== Estructura de base de datos =====
 +
 +==== Tipos de comida ====
 +
 +  * id
 +  * nombre
 +  * slug
 +  * descripción
  
 ==== Comidas ==== ==== Comidas ====
Línea 72: Línea 79:
 </WRAP> </WRAP>
  
 +===== Tipos de comida =====
 +
 +Según el momento del día:
 +
 +  * Desayuno
 +  * Comida
 +  * Cena
 +  * Tentempié / picoteo / snack
 +
 +==== Migración ====
 +
 +<code>
 +php artisan make:migration create_meal_types_table
 +</code>
 +
 +Contenido del fichero ''database/migrations/2025_03_30_164255_create_meal_types_table.php'':
 +
 +<code php>
 +<?php
 +
 +use Illuminate\Database\Migrations\Migration;
 +use Illuminate\Database\Schema\Blueprint;
 +use Illuminate\Support\Facades\Schema;
 +
 +return new class extends Migration
 +{
 +    /**
 +     * Run the migrations.
 +     */
 +    public function up(): void
 +    {
 +        Schema::create('meal_types', function (Blueprint $table) {
 +            $table->id();
 +            $table->string('name');
 +            $table->string('slug')->unique();
 +            $table->text('description')->nullable();
 +            $table->timestamps();
 +        });
 +    }
 +
 +    /**
 +     * Reverse the migrations.
 +     */
 +    public function down(): void
 +    {
 +        Schema::dropIfExists('meal_types');
 +    }
 +};
 +
 +</code>
 +
 +==== Modelo ====
 +
 +Lo creamos con: 
 +
 +<code>
 +php artisan make:model MealType 
 +</code>
 +
 +Contenido del fichero generado ''app/Models/MealType.php'':
 +
 +<code php>
 +<?php
 +
 +namespace App\Models;
 +
 +use Illuminate\Database\Eloquent\Model;
 +
 +class MealType extends Model
 +{
 +    //
 +}
 +
 +</code>
 ===== Categorías ===== ===== Categorías =====
  
Línea 252: Línea 333:
 <code php> <code php>
 <?php <?php
- +
 namespace App\Models; namespace App\Models;
- +
 use Illuminate\Database\Eloquent\Model; use Illuminate\Database\Eloquent\Model;
 use Illuminate\Database\Eloquent\Factories\HasFactory; use Illuminate\Database\Eloquent\Factories\HasFactory;
-  +use Illuminate\Database\Eloquent\Relations\BelongsTo; 
-  +use Illuminate\Database\Eloquent\Relations\BelongsToMany; 
-class Category extends Model+ 
 +class Meal extends Model
 { {
     use HasFactory;     use HasFactory;
-  + 
-    protected $fillable = ['name', 'color']; +    protected $fillable = [ 
-  +        'name', 
-    public function meals()+        'description'
 +        'date_time', 
 +        'meal_type', 
 +        'user_id' 
 +    ]; 
 + 
 +    protected $casts = [ 
 +        'date_time' => 'datetime', 
 +    ]; 
 + 
 +    // Relaciones 
 +    public function user(): BelongsTo
     {     {
-        return $this->hasMany(Meal::class);+        return $this->belongsTo(User::class);
     }     }
-  + 
-    public function ingredients()+    public function category(): BelongsTo
     {     {
-        return $this->hasMany(Ingredient::class);+        return $this->belongsTo(Category::class); 
 +    } 
 + 
 +    public function ingredients(): BelongsToMany 
 +    { 
 +        return $this->belongsToMany(Ingredient::class) 
 +            ->withPivot('quantity'
 +            ->withTimestamps(); 
 +    } 
 + 
 +    public function tags(): BelongsToMany 
 +    { 
 +        return $this->belongsToMany(Tag::class) 
 +            ->withTimestamps(); 
 +    } 
 + 
 +    // Accesor para el lugar 
 +    public function getLocationAttribute(): string 
 +    { 
 +        return match($this->location_type) { 
 +            'casa' => 'En casa', 
 +            'casa_familiar' => 'Casa de familiar', 
 +            'restaurante' => $this->restaurant ?? 'Restaurante', 
 +            default => 'Desconocido' 
 +        };
     }     }
 } }
proyectos/registro_comidas.1743347959.txt.gz · Última modificación: por tempwin