proyectos:registro_comidas
Diferencias
Muestra las diferencias entre dos versiones de la página.
| Ambos lados, revisión anteriorRevisión previaPróxima revisión | Revisión previa | ||
| proyectos:registro_comidas [2025/03/30 17:14] – [Comidas] tempwin | proyectos: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: | ||
| </ | </ | ||
| + | ===== Tipos de comida ===== | ||
| + | |||
| + | Según el momento del día: | ||
| + | |||
| + | * Desayuno | ||
| + | * Comida | ||
| + | * Cena | ||
| + | * Tentempié / picoteo / snack | ||
| + | |||
| + | ==== Migración ==== | ||
| + | |||
| + | < | ||
| + | php artisan make: | ||
| + | </ | ||
| + | |||
| + | Contenido del fichero '' | ||
| + | |||
| + | <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:: | ||
| + | $table-> | ||
| + | $table-> | ||
| + | $table-> | ||
| + | $table-> | ||
| + | $table-> | ||
| + | }); | ||
| + | } | ||
| + | |||
| + | /** | ||
| + | * Reverse the migrations. | ||
| + | */ | ||
| + | public function down(): void | ||
| + | { | ||
| + | Schema:: | ||
| + | } | ||
| + | }; | ||
| + | |||
| + | </ | ||
| + | |||
| + | ==== Modelo ==== | ||
| + | |||
| + | Lo creamos con: | ||
| + | |||
| + | < | ||
| + | php artisan make:model MealType | ||
| + | </ | ||
| + | |||
| + | Contenido del fichero generado '' | ||
| + | |||
| + | <code php> | ||
| + | <?php | ||
| + | |||
| + | namespace App\Models; | ||
| + | |||
| + | use Illuminate\Database\Eloquent\Model; | ||
| + | |||
| + | class MealType extends Model | ||
| + | { | ||
| + | // | ||
| + | } | ||
| + | |||
| + | </ | ||
| ===== Categorías ===== | ===== Categorías ===== | ||
| Línea 77: | Línea 158: | ||
| < | < | ||
| - | php atisan | + | php artisan |
| </ | </ | ||
| Línea 197: | Línea 278: | ||
| ===== Comidas ===== | ===== Comidas ===== | ||
| + | |||
| + | ==== Migración ==== | ||
| **Migración** para la tabla de las **comidas**: | **Migración** para la tabla de las **comidas**: | ||
| Línea 208: | Línea 291: | ||
| <code php> | <code php> | ||
| <?php | <?php | ||
| - | + | ||
| use Illuminate\Database\Migrations\Migration; | use Illuminate\Database\Migrations\Migration; | ||
| use Illuminate\Database\Schema\Blueprint; | use Illuminate\Database\Schema\Blueprint; | ||
| use Illuminate\Support\Facades\Schema; | use Illuminate\Support\Facades\Schema; | ||
| - | + | ||
| return new class extends Migration | return new class extends Migration | ||
| { | { | ||
| Línea 220: | Línea 303: | ||
| public function up(): void | public function up(): void | ||
| { | { | ||
| - | Schema:: | + | Schema:: |
| $table-> | $table-> | ||
| - | $table-> | + | |
| - | $table->string('color' | + | |
| + | $table->text(' | ||
| + | $table->dateTime('date_time' | ||
| + | $table-> | ||
| + | $table-> | ||
| + | $table-> | ||
| + | $table-> | ||
| $table-> | $table-> | ||
| }); | }); | ||
| } | } | ||
| - | + | ||
| /** | /** | ||
| * Reverse the migrations. | * Reverse the migrations. | ||
| Línea 233: | Línea 322: | ||
| public function down(): void | public function down(): void | ||
| { | { | ||
| - | Schema:: | + | Schema:: |
| } | } | ||
| - | } | + | }; |
| </ | </ | ||
| - | Modelo | + | ==== Modelo |
| + | Modelo para las categorías ('' | ||
| + | |||
| + | <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 | + | |
| + | class Meal extends Model | ||
| { | { | ||
| use HasFactory; | use HasFactory; | ||
| - | + | ||
| - | protected $fillable = [' | + | protected $fillable = [ |
| - | + | | |
| - | public function | + | |
| + | ' | ||
| + | ' | ||
| + | ' | ||
| + | | ||
| + | |||
| + | protected $casts = [ | ||
| + | ' | ||
| + | ]; | ||
| + | |||
| + | // Relaciones | ||
| + | public function | ||
| { | { | ||
| - | return $this->hasMany(Meal::class); | + | return $this->belongsTo(User::class); |
| } | } | ||
| - | + | ||
| - | public function | + | public function |
| { | { | ||
| - | return $this->hasMany(Ingredient:: | + | return $this->belongsTo(Category:: |
| + | } | ||
| + | |||
| + | public function ingredients(): | ||
| + | { | ||
| + | return $this-> | ||
| + | -> | ||
| + | -> | ||
| + | } | ||
| + | |||
| + | public function tags(): BelongsToMany | ||
| + | { | ||
| + | return $this-> | ||
| + | -> | ||
| + | } | ||
| + | |||
| + | // Accesor para el lugar | ||
| + | public function getLocationAttribute(): | ||
| + | { | ||
| + | return match($this-> | ||
| + | ' | ||
| + | ' | ||
| + | ' | ||
| + | default => ' | ||
| + | }; | ||
| } | } | ||
| } | } | ||
| + | </ | ||
| - | Controlador | + | ==== Controlador |
| Creamos un controlador asociado al modelo de las categorías: | Creamos un controlador asociado al modelo de las categorías: | ||
| - | php artisan make: | + | < |
| + | php artisan make: | ||
| + | </ | ||
| - | Se creará el fichero app/ | + | Se creará el fichero |
| - | | + | * index() |
| - | create() | + | |
| - | store() | + | |
| - | show() | + | |
| - | edit() | + | |
| - | update() | + | |
| - | destroy() | + | |
| - | Vistas | + | ==== Vistas |
| - | Creamos las vistas para todos los métodos del controlador menos store(), update() y destroy(): | + | Creamos las vistas para todos los métodos del controlador menos '' |
| - | php artisan make: | + | < |
| + | php artisan make: | ||
| - | php artisan make: | + | php artisan make: |
| - | php artisan make: | + | php artisan make: |
| - | php artisan make: | + | php artisan make: |
| + | </ | ||
| - | Se habrán creado los siguientes ficheros en el directorio resources/ | + | Se habrán creado los siguientes ficheros en el directorio |
| - | | + | * '' |
| - | create.blade.php | + | * '' |
| - | show.blade.php | + | * '' |
| - | edit.blade.php | + | * '' |
| - | Rutas | + | ==== Rutas ==== |
| - | En el fichero routes/ | + | En el fichero |
| - | Route:: | + | <code php> |
| + | Route:: | ||
| + | </ | ||
| Podemos verificar con el siguiente comando las rutas registradas: | Podemos verificar con el siguiente comando las rutas registradas: | ||
| + | < | ||
| php artisan route:list | php artisan route:list | ||
| + | </ | ||
proyectos/registro_comidas.1743347681.txt.gz · Última modificación: por tempwin
