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 15:37] – [Base de datos] 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 121: | Línea 202: | ||
| namespace App\Models; | namespace App\Models; | ||
| - | use Illuminate\Database\Eloquent\Factories\HasFactory; | ||
| use Illuminate\Database\Eloquent\Model; | use Illuminate\Database\Eloquent\Model; | ||
| + | use Illuminate\Database\Eloquent\Factories\HasFactory; | ||
| + | |||
| class Category extends Model | class Category extends Model | ||
| Línea 143: | Línea 225: | ||
| </ | </ | ||
| - | === Controlador === | + | ==== Controlador ==== |
| + | |||
| + | Creamos un controlador asociado al modelo de las categorías: | ||
| < | < | ||
| php artisan make: | php artisan make: | ||
| </ | </ | ||
| + | |||
| + | Se creará el fichero '' | ||
| + | |||
| + | * '' | ||
| + | * '' | ||
| + | * '' | ||
| + | * '' | ||
| + | * '' | ||
| + | * '' | ||
| + | * '' | ||
| + | |||
| + | ==== Vistas ==== | ||
| + | |||
| + | Creamos las vistas para todos los métodos del controlador menos '' | ||
| + | |||
| + | < | ||
| + | php artisan make:view categories.index | ||
| + | |||
| + | php artisan make:view categories.create | ||
| + | |||
| + | php artisan make:view categories.show | ||
| + | |||
| + | php artisan make:view categories.edit | ||
| + | </ | ||
| + | |||
| + | Se habrán creado los siguientes ficheros en el directorio '' | ||
| + | |||
| + | * '' | ||
| + | * '' | ||
| + | * '' | ||
| + | * '' | ||
| + | ==== Rutas ==== | ||
| + | |||
| + | En el fichero '' | ||
| + | |||
| + | < | ||
| + | Route:: | ||
| + | </ | ||
| + | |||
| + | Podemos verificar con el siguiente comando las rutas registradas: | ||
| + | |||
| + | < | ||
| + | php artisan route:list | ||
| + | </ | ||
| + | |||
| + | ===== Comidas ===== | ||
| + | |||
| + | ==== Migración ==== | ||
| + | |||
| + | **Migración** para la tabla de las **comidas**: | ||
| + | |||
| + | < | ||
| + | php artisan make: | ||
| + | </ | ||
| + | |||
| + | Fichero con la migración ('' | ||
| + | |||
| + | <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-> | ||
| + | $table-> | ||
| + | $table-> | ||
| + | $table-> | ||
| + | $table-> | ||
| + | $table-> | ||
| + | }); | ||
| + | } | ||
| + | |||
| + | /** | ||
| + | * Reverse the migrations. | ||
| + | */ | ||
| + | public function down(): void | ||
| + | { | ||
| + | Schema:: | ||
| + | } | ||
| + | }; | ||
| + | </ | ||
| + | |||
| + | ==== Modelo ==== | ||
| + | |||
| + | Modelo para las categorías ('' | ||
| + | |||
| + | <code php> | ||
| + | <?php | ||
| + | |||
| + | namespace App\Models; | ||
| + | |||
| + | use Illuminate\Database\Eloquent\Model; | ||
| + | use Illuminate\Database\Eloquent\Factories\HasFactory; | ||
| + | use Illuminate\Database\Eloquent\Relations\BelongsTo; | ||
| + | use Illuminate\Database\Eloquent\Relations\BelongsToMany; | ||
| + | |||
| + | class Meal extends Model | ||
| + | { | ||
| + | use HasFactory; | ||
| + | |||
| + | protected $fillable = [ | ||
| + | ' | ||
| + | ' | ||
| + | ' | ||
| + | ' | ||
| + | ' | ||
| + | ]; | ||
| + | |||
| + | protected $casts = [ | ||
| + | ' | ||
| + | ]; | ||
| + | |||
| + | // Relaciones | ||
| + | public function user(): BelongsTo | ||
| + | { | ||
| + | return $this-> | ||
| + | } | ||
| + | |||
| + | public function category(): BelongsTo | ||
| + | { | ||
| + | return $this-> | ||
| + | } | ||
| + | |||
| + | public function ingredients(): | ||
| + | { | ||
| + | return $this-> | ||
| + | -> | ||
| + | -> | ||
| + | } | ||
| + | |||
| + | public function tags(): BelongsToMany | ||
| + | { | ||
| + | return $this-> | ||
| + | -> | ||
| + | } | ||
| + | |||
| + | // Accesor para el lugar | ||
| + | public function getLocationAttribute(): | ||
| + | { | ||
| + | return match($this-> | ||
| + | ' | ||
| + | ' | ||
| + | ' | ||
| + | default => ' | ||
| + | }; | ||
| + | } | ||
| + | } | ||
| + | </ | ||
| + | |||
| + | ==== Controlador ==== | ||
| + | |||
| + | Creamos un controlador asociado al modelo de las categorías: | ||
| + | |||
| + | < | ||
| + | php artisan make: | ||
| + | </ | ||
| + | |||
| + | Se creará el fichero '' | ||
| + | |||
| + | * index() | ||
| + | * create() | ||
| + | * store() | ||
| + | * show() | ||
| + | * edit() | ||
| + | * update() | ||
| + | * destroy() | ||
| + | |||
| + | ==== Vistas ==== | ||
| + | |||
| + | Creamos las vistas para todos los métodos del controlador menos '' | ||
| + | |||
| + | < | ||
| + | php artisan make:view meals.index | ||
| + | |||
| + | php artisan make:view meals.create | ||
| + | |||
| + | php artisan make:view meals.show | ||
| + | |||
| + | php artisan make:view meals.edit | ||
| + | </ | ||
| + | |||
| + | Se habrán creado los siguientes ficheros en el directorio '' | ||
| + | |||
| + | * '' | ||
| + | * '' | ||
| + | * '' | ||
| + | * '' | ||
| + | |||
| + | ==== Rutas ==== | ||
| + | |||
| + | En el fichero '' | ||
| + | |||
| + | <code php> | ||
| + | Route:: | ||
| + | </ | ||
| + | |||
| + | Podemos verificar con el siguiente comando las rutas registradas: | ||
| + | |||
| + | < | ||
| + | php artisan route:list | ||
| + | </ | ||
| + | |||
proyectos/registro_comidas.1743341856.txt.gz · Última modificación: por tempwin
