我是否需要为每个重复输入创建一个新 ID?
Do I need to create a new id for every repeating input?
为了解释,我正在为用户创建一种创建膳食计划的方法。像这样;
,简单说明一下。它将被设计为用户可以通过交互式单击 3x3 table 中的日期来填写当天的膳食,这将更改天数以使用 j 查询在下面输入膳食。
添加早餐、小吃、午餐、小吃、晚餐和小吃时,我的困惑就来了。由于一周中的每一天都需要它,我是否需要像这样为每一天创建一个不同的 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 添加到我的视图中时,它应该是这样的吗? (这仅适用于周一和周二)
<div class="page slide-page">
<div id="day_id"
class="title">Monday:</div>
<div class="form-group row">
<label for="caption" class="col-md-4 col-form-label">Breakfast</label>
<input id="meal"
type="text"
class="form-control{{ $errors->has('meal') ? ' is-invalid' : '' }}"
name="meal"
value="{{ old('meal') }}"
autocomplete="name" autofocus>
@if ($errors->has('meal'))
<span class="invalid-feedback" role="alert">
<strong>{{ $errors->first('meal') }}</strong>
</span>
@endif
</div>
<div class="form-group row">
<label for="caption" class="col-md-4 col-form-label">Snack</label>
<input id="meal"
type="text"
class="form-control{{ $errors->has('meal') ? ' is-invalid' : '' }}"
name="meal"
value="{{ old('meal') }}"
autocomplete="name" autofocus>
@if ($errors->has('meal'))
<span class="invalid-feedback" role="alert">
<strong>{{ $errors->first('meal') }}</strong>
</span>
@endif
</div>
<div class="form-group row">
<label for="caption" class="col-md-4 col-form-label">Lunch</label>
<input id="meal"
type="text"
class="form-control{{ $errors->has('meal') ? ' is-invalid' : '' }}"
name="meal"
value="{{ old('meal') }}"
autocomplete="name" autofocus>
@if ($errors->has('meal'))
<span class="invalid-feedback" role="alert">
<strong>{{ $errors->first('meal') }}</strong>
</span>
@endif
</div>
<div class="form-group row">
<label for="caption" class="col-md-4 col-form-label">Snack</label>
<input id="meal"
type="text"
class="form-control{{ $errors->has('meal') ? ' is-invalid' : '' }}"
name="meal"
value="{{ old('meal') }}"
autocomplete="name" autofocus>
@if ($errors->has('meal'))
<span class="invalid-feedback" role="alert">
<strong>{{ $errors->first('meal') }}</strong>
</span>
@endif
</div>
<div class="form-group row">
<label for="caption" class="col-md-4 col-form-label">Dinner</label>
<input id="meal"
type="text"
class="form-control{{ $errors->has('meal') ? ' is-invalid' : '' }}"
name="meal"
value="{{ old('meal') }}"
autocomplete="name" autofocus>
@if ($errors->has('dinner'))
<span class="invalid-feedback" role="alert">
<strong>{{ $errors->first('dinner') }}</strong>
</span>
@endif
</div>
<div class="form-group row">
<label for="caption" class="col-md-4 col-form-label">Snack</label>
<input id="meal"
type="text"
class="form-control{{ $errors->has('snack') ? ' is-invalid' : '' }}"
name="snack"
value="{{ old('snack') }}"
autocomplete="name" autofocus>
@if ($errors->has('snack'))
<span class="invalid-feedback" role="alert">
<strong>{{ $errors->first('snack') }}</strong>
</span>
@endif
</div>
</div>
<div class="page">
<div id="day_id"
class="title">Tuesday:</div>
<div class="form-group row">
<label for="caption" class="col-md-4 col-form-label">Breakfast</label>
<input id="meal"
type="text"
class="form-control{{ $errors->has('meal') ? ' is-invalid' : '' }}"
name="meal"
value="{{ old('meal') }}"
autocomplete="name" autofocus>
@if ($errors->has('meal'))
<span class="invalid-feedback" role="alert">
<strong>{{ $errors->first('meal') }}</strong>
</span>
@endif
</div>
<div class="form-group row">
<label for="caption" class="col-md-4 col-form-label">Snack</label>
<input id="meal"
type="text"
class="form-control{{ $errors->has('meal') ? ' is-invalid' : '' }}"
name="meal"
value="{{ old('meal') }}"
autocomplete="name" autofocus>
@if ($errors->has('meal'))
<span class="invalid-feedback" role="alert">
<strong>{{ $errors->first('meal') }}</strong>
</span>
@endif
</div>
<div class="form-group row">
<label for="caption" class="col-md-4 col-form-label">Lunch</label>
<input id="meal"
type="text"
class="form-control{{ $errors->has('meal') ? ' is-invalid' : '' }}"
name="meal"
value="{{ old('meal') }}"
autocomplete="name" autofocus>
@if ($errors->has('meal'))
<span class="invalid-feedback" role="alert">
<strong>{{ $errors->first('meal') }}</strong>
</span>
@endif
</div>
<div class="form-group row">
<label for="caption" class="col-md-4 col-form-label">Snack</label>
<input id="meal"
type="text"
class="form-control{{ $errors->has('meal') ? ' is-invalid' : '' }}"
name="meal"
value="{{ old('meal') }}"
autocomplete="name" autofocus>
@if ($errors->has('meal'))
<span class="invalid-feedback" role="alert">
<strong>{{ $errors->first('meal') }}</strong>
</span>
@endif
</div>
<div class="form-group row">
<label for="caption" class="col-md-4 col-form-label">Dinner</label>
<input id="meal"
type="text"
class="form-control{{ $errors->has('meal') ? ' is-invalid' : '' }}"
name="meal"
value="{{ old('meal') }}"
autocomplete="name" autofocus>
@if ($errors->has('dinner'))
<span class="invalid-feedback" role="alert">
<strong>{{ $errors->first('dinner') }}</strong>
</span>
@endif
</div>
<div class="form-group row">
<label for="caption" class="col-md-4 col-form-label">Snack</label>
<input id="meal"
type="text"
class="form-control{{ $errors->has('snack') ? ' is-invalid' : '' }}"
name="snack"
value="{{ old('snack') }}"
autocomplete="name" autofocus>
@if ($errors->has('snack'))
<span class="invalid-feedback" role="alert">
<strong>{{ $errors->first('snack') }}</strong>
</span>
@endif
</div>
</div>
如果您需要将这些数据集中在一个 table 中。那么最好使用较新数据库版本中可用的 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();
});
注意:如果您不打算使用单独的主 table 来节省时间,请跳过 day_id 外键并将其用作字符串。理想情况下将其保留为 id 将有助于您轻松查询。
如果您不需要复杂的查询(例如获取所有用户的午餐详细信息或他们的早餐详细信息等),这将有效。如果您需要这样的东西,最好将数据拆分为第二个 table 以进行维护关注点分离和数据库设计原则。
为了解释,我正在为用户创建一种创建膳食计划的方法。像这样;
添加早餐、小吃、午餐、小吃、晚餐和小吃时,我的困惑就来了。由于一周中的每一天都需要它,我是否需要像这样为每一天创建一个不同的 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 添加到我的视图中时,它应该是这样的吗? (这仅适用于周一和周二)
<div class="page slide-page">
<div id="day_id"
class="title">Monday:</div>
<div class="form-group row">
<label for="caption" class="col-md-4 col-form-label">Breakfast</label>
<input id="meal"
type="text"
class="form-control{{ $errors->has('meal') ? ' is-invalid' : '' }}"
name="meal"
value="{{ old('meal') }}"
autocomplete="name" autofocus>
@if ($errors->has('meal'))
<span class="invalid-feedback" role="alert">
<strong>{{ $errors->first('meal') }}</strong>
</span>
@endif
</div>
<div class="form-group row">
<label for="caption" class="col-md-4 col-form-label">Snack</label>
<input id="meal"
type="text"
class="form-control{{ $errors->has('meal') ? ' is-invalid' : '' }}"
name="meal"
value="{{ old('meal') }}"
autocomplete="name" autofocus>
@if ($errors->has('meal'))
<span class="invalid-feedback" role="alert">
<strong>{{ $errors->first('meal') }}</strong>
</span>
@endif
</div>
<div class="form-group row">
<label for="caption" class="col-md-4 col-form-label">Lunch</label>
<input id="meal"
type="text"
class="form-control{{ $errors->has('meal') ? ' is-invalid' : '' }}"
name="meal"
value="{{ old('meal') }}"
autocomplete="name" autofocus>
@if ($errors->has('meal'))
<span class="invalid-feedback" role="alert">
<strong>{{ $errors->first('meal') }}</strong>
</span>
@endif
</div>
<div class="form-group row">
<label for="caption" class="col-md-4 col-form-label">Snack</label>
<input id="meal"
type="text"
class="form-control{{ $errors->has('meal') ? ' is-invalid' : '' }}"
name="meal"
value="{{ old('meal') }}"
autocomplete="name" autofocus>
@if ($errors->has('meal'))
<span class="invalid-feedback" role="alert">
<strong>{{ $errors->first('meal') }}</strong>
</span>
@endif
</div>
<div class="form-group row">
<label for="caption" class="col-md-4 col-form-label">Dinner</label>
<input id="meal"
type="text"
class="form-control{{ $errors->has('meal') ? ' is-invalid' : '' }}"
name="meal"
value="{{ old('meal') }}"
autocomplete="name" autofocus>
@if ($errors->has('dinner'))
<span class="invalid-feedback" role="alert">
<strong>{{ $errors->first('dinner') }}</strong>
</span>
@endif
</div>
<div class="form-group row">
<label for="caption" class="col-md-4 col-form-label">Snack</label>
<input id="meal"
type="text"
class="form-control{{ $errors->has('snack') ? ' is-invalid' : '' }}"
name="snack"
value="{{ old('snack') }}"
autocomplete="name" autofocus>
@if ($errors->has('snack'))
<span class="invalid-feedback" role="alert">
<strong>{{ $errors->first('snack') }}</strong>
</span>
@endif
</div>
</div>
<div class="page">
<div id="day_id"
class="title">Tuesday:</div>
<div class="form-group row">
<label for="caption" class="col-md-4 col-form-label">Breakfast</label>
<input id="meal"
type="text"
class="form-control{{ $errors->has('meal') ? ' is-invalid' : '' }}"
name="meal"
value="{{ old('meal') }}"
autocomplete="name" autofocus>
@if ($errors->has('meal'))
<span class="invalid-feedback" role="alert">
<strong>{{ $errors->first('meal') }}</strong>
</span>
@endif
</div>
<div class="form-group row">
<label for="caption" class="col-md-4 col-form-label">Snack</label>
<input id="meal"
type="text"
class="form-control{{ $errors->has('meal') ? ' is-invalid' : '' }}"
name="meal"
value="{{ old('meal') }}"
autocomplete="name" autofocus>
@if ($errors->has('meal'))
<span class="invalid-feedback" role="alert">
<strong>{{ $errors->first('meal') }}</strong>
</span>
@endif
</div>
<div class="form-group row">
<label for="caption" class="col-md-4 col-form-label">Lunch</label>
<input id="meal"
type="text"
class="form-control{{ $errors->has('meal') ? ' is-invalid' : '' }}"
name="meal"
value="{{ old('meal') }}"
autocomplete="name" autofocus>
@if ($errors->has('meal'))
<span class="invalid-feedback" role="alert">
<strong>{{ $errors->first('meal') }}</strong>
</span>
@endif
</div>
<div class="form-group row">
<label for="caption" class="col-md-4 col-form-label">Snack</label>
<input id="meal"
type="text"
class="form-control{{ $errors->has('meal') ? ' is-invalid' : '' }}"
name="meal"
value="{{ old('meal') }}"
autocomplete="name" autofocus>
@if ($errors->has('meal'))
<span class="invalid-feedback" role="alert">
<strong>{{ $errors->first('meal') }}</strong>
</span>
@endif
</div>
<div class="form-group row">
<label for="caption" class="col-md-4 col-form-label">Dinner</label>
<input id="meal"
type="text"
class="form-control{{ $errors->has('meal') ? ' is-invalid' : '' }}"
name="meal"
value="{{ old('meal') }}"
autocomplete="name" autofocus>
@if ($errors->has('dinner'))
<span class="invalid-feedback" role="alert">
<strong>{{ $errors->first('dinner') }}</strong>
</span>
@endif
</div>
<div class="form-group row">
<label for="caption" class="col-md-4 col-form-label">Snack</label>
<input id="meal"
type="text"
class="form-control{{ $errors->has('snack') ? ' is-invalid' : '' }}"
name="snack"
value="{{ old('snack') }}"
autocomplete="name" autofocus>
@if ($errors->has('snack'))
<span class="invalid-feedback" role="alert">
<strong>{{ $errors->first('snack') }}</strong>
</span>
@endif
</div>
</div>
如果您需要将这些数据集中在一个 table 中。那么最好使用较新数据库版本中可用的 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();
});
注意:如果您不打算使用单独的主 table 来节省时间,请跳过 day_id 外键并将其用作字符串。理想情况下将其保留为 id 将有助于您轻松查询。
如果您不需要复杂的查询(例如获取所有用户的午餐详细信息或他们的早餐详细信息等),这将有效。如果您需要这样的东西,最好将数据拆分为第二个 table 以进行维护关注点分离和数据库设计原则。