To explain I am creating a way for users to create a meal plan. like so;
, To briefly explain. It will be designed so the user would fill in the meals of the day by interactively clicking on the days in the 3×3 table, which would change the days to fill in below enter meals by using j query.
My confusion comes in to play when adding breakfast, snack, lunch, snack, dinner and snack. Since its needed for each day of the week, would i need to create a different id for each day like this;
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'); } }
I feel like this is a long winded way of doing it as i would then have 6 different breakfast id’s. Is there a way to write it in a shorter and cleaner way?
When posting multiple instances of the same input where does it save in the database?
When using json and then adding the id into my view is this how it should look? (this is for just Monday and Tuesday)
<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>
Advertisement
Answer
If you need to have these data in a single table. Then it would be a great idea to use the JSON data structure available in newer database versions to store your meal data. For example:
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(); });
Note: If you are not planning in saving days using a separate master table, skip day_id foreign key & use it as a string. Ideally keeping it as id would help you query easily.
This will work if you don’t need complex queries like get all users lunch details or their breakfast details etc. If you need such things it would be ideal to split the data into second table to maintain the separation of concerns & DB design principles.