Php 是否需要为每个重复输入创建新id?

Php 是否需要为每个重复输入创建新id?,php,laravel,post,input,database-design,Php,Laravel,Post,Input,Database Design,为了说明这一点,我正在为用户创建一种创建膳食计划的方法。就这样, ,简单解释一下。它的设计使用户可以通过交互单击3x3表中的天数来填写当天的膳食,这将更改要填写的天数,然后使用j query输入膳食 当添加早餐、零食、午餐、零食、晚餐和零食时,我的困惑就来了。因为一周中的每一天都需要它,我是否需要为每一天创建一个不同的id,就像这样 class CreateMealPlansTable extends Migration { /** * Run the migratio

为了说明这一点,我正在为用户创建一种创建膳食计划的方法。就这样,

,简单解释一下。它的设计使用户可以通过交互单击3x3表中的天数来填写当天的膳食,这将更改要填写的天数,然后使用j query输入膳食

当添加早餐、零食、午餐、零食、晚餐和零食时,我的困惑就来了。因为一周中的每一天都需要它,我是否需要为每一天创建一个不同的id,就像这样

    class CreateMealPlansTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('meal_plans', function (Blueprint $table) {
            $table->id();
            $table->unsignedBigInteger('user_id');
            $table->unsignedBigInteger('recipes_id');
            $table->mediumText('monday'); //medium height text
            $table->mediumText('breakfast1'); //medium height text
            $table->mediumText('snack1'); //medium height text
            $table->mediumText('lunch1'); //medium height text
            $table->mediumText('snack1a'); //medium height text
            $table->mediumText('dinner1'); //medium height text
            $table->mediumText('snack1b'); //medium height text
            $table->mediumText('tuesday'); //medium height text
            $table->mediumText('breakfast2'); //medium height text
            $table->mediumText('snack2'); //medium height text
            $table->mediumText('lunch2'); //medium height text
            $table->mediumText('snack2a'); //medium height text
            $table->mediumText('dinner2'); //medium height text
            $table->mediumText('snack2b'); //medium height text


            $table->timestamps();
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::dropIfExists('meal_plans');
    }
}
我觉得这是一个冗长的做法,因为我会有6个不同的早餐id。有没有办法把它写得更短更干净

发布同一输入的多个实例时,它保存在数据库中的什么位置

当使用json并将id添加到我的视图中时,应该是这样吗?(仅适用于周一和周二)


星期一:
早餐
@如果($errors->has('mean'))
{{$errors->first('meat')}
@恩迪夫
小吃
@如果($errors->has('mean'))
{{$errors->first('meat')}
@恩迪夫
午餐
@如果($errors->has('mean'))
{{$errors->first('meat')}
@恩迪夫
小吃
@如果($errors->has('mean'))
{{$errors->first('meat')}
@恩迪夫
晚餐
@如果($errors->has('dinner'))
{{$errors->first('dinner')}
@恩迪夫
小吃
@如果($errors->has('snack'))
{{$errors->first('snack')}
@恩迪夫
星期二:
早餐
@如果($errors->has('mean'))
{{$errors->first('meat')}
@恩迪夫
小吃
@如果($errors->has('mean'))
{{$errors->first('meat')}
@恩迪夫
午餐
@如果($errors->has('mean'))
{{$errors->first('meat')}
@恩迪夫
小吃
@如果($errors->has('mean'))
{{$errors->first('meat')}
@恩迪夫
晚餐
@如果($errors->has('dinner'))
{{$errors->first('dinner')}
@恩迪夫
小吃
@如果($errors->has('snack'))
{{$errors->first('snack')}
@恩迪夫

如果需要将这些数据放在一个表中。那么,最好使用新数据库版本中可用的JSON数据结构来存储您的膳食数据。例如:

Schema::create('meal_plans', function (Blueprint $table) {
    $table->id();
    $table->unsignedBigInteger('user_id');
    $table->unsignedBigInteger('recipes_id');
    $table->unsignedBigInteger('day_id');
    $table->json('meals');
    $table->timestamps();
});
注意:如果您不打算使用单独的主表保存天数,请跳过day_id外键并将其用作字符串。理想情况下,将其保留为id将帮助您轻松查询


如果您不需要复杂的查询,如获取所有用户的午餐详细信息或早餐详细信息等,这将起作用。如果您需要这些信息,最好将数据分割到第二个表中,以保持关注点和数据库设计原则的分离。

任何时候您需要硬编码
something1
something2
something3
后退一步,重新评估你的方法。在你的情况下,你的数据库会有点复杂,每顿
饭_
Schema::create('meal_plans', function (Blueprint $table) {
    $table->id();
    $table->unsignedBigInteger('user_id');
    $table->unsignedBigInteger('recipes_id');
    $table->unsignedBigInteger('day_id');
    $table->json('meals');
    $table->timestamps();
});