如何正确设置用户设置膳食计划的方式?
How do I correctly set up a way for users to set up a meal plan?
我正在尝试为用户建立一个基本的方式来制定一周的膳食计划。我已经尝试过几种方法,但由于这是我的第一个项目,我已经严重卡住了 atm。
所以下面我将展示我所采取的步骤并解释我的想法。
create_meal_plans_table.php
我喜欢让 table 尽可能简单,但这可能是我的失败。我最初将星期一到星期五的日子显示为单独的字符串,但它很乱,因此决定将其放在自己的日期 table.
中
然后我添加了一个标题,这样他们就可以在用户个人资料中从下拉列表中选择一个特定的膳食计划,并且会显示出来。
剩下的不言而喻。
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
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('day_id');
$table->string('title');
$table->string('breakfast');
$table->string('lunch');
$table->string('dinner');
$table->string('snack');
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('meal_plans');
}
}
create_days_table
我不确定将日期保留为字符串是否正确。
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
class CreateDaysTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('days', function (Blueprint $table) {
$table->id();
$table->mediumText('monday'); //medium height text
$table->mediumText('tuesday'); //medium height text
$table->mediumText('wednesday'); //medium height text
$table->mediumText('thursday'); //medium height text
$table->mediumText('friday'); //medium height text
$table->mediumText('saturday'); //medium height text
$table->mediumText('sunday'); //medium height text
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('days');
}
}
Meal/create.blade.php
我不会对此进行解释,而是会截取它的外观并在下面显示。
@extends('layouts.app')
@section('content')
<head>
<style>
table .button {
background-color: transparent;
border: none;
color: black;
padding: 16px 32px;
text-align: center;
text-decoration: none;
display: inline-block;
font-size: 16px;
margin: 4px 2px;
/*transition-duration: 0.4s;*/
cursor: pointer;
}
table {
margin-left: auto;
margin-right: auto;
}
td {
width: 8rem;
height: 4rem;
border: 1px solid #ccc;
text-align: center;
}
th {
background: lightblue;
border-color: white;
}
body {
padding: 1rem;
}
</style>
</head>
<body>
<div class="container">
<form action="/mp" enctype="multipart/form-data" method="post">
@csrf
<div class="row">
<div class="col-8 offset-2">
<div class="row pb-3">
<h1>Add Meal Plan</h1>
</div>
<div class="row">
<table class="table" >
<tr>
<td><button class="button-1 button">MON</button></td>
<td><button class="button-2 button">TUE</button></td>
<td><button class="button-3 button">WED</button></td>
</tr>
<tr>
<td><button class="button-4 button">THURS</button></td>
<td><button class="button-5 button">FRI</button></td>
<td><button class="button-6 button">SAT</button></td>
</tr>
<tr>
<td></td>
<td><button class="button-7 button">SUN</button></td>
<td></td>
</tr>
</table>
</div>
<header>Enter Meals</header>
<div class="form-outer">
<form action="/mp" enctype="multipart/form-data" method="post">
@csrf
<div class="field">
<label for="caption" class="col-md-4 col-form-label">Day</label>
<select>
<option> --Select Tags--</option>
<option id="monday"
class="form-control"
name="monday">
Monday</option>
<option id="tuesday"
class="form-control"
name="tuesday">
Tuesday</option>
<option id="wednesday"
class="form-control"
name="wednesday">
Wednesday</option>
<option id="thursday"
class="form-control"
name="thursday">
Thursday</option>
<option id="friday"
class="form-control"
name="friday">
Friday</option>
<option id="saturday"
class="form-control"
name="saturday"
>Saturday</option>
<option id="sunday"
class="form-control"
name="sunday"
>Sunday</option>
</select>
@if ($errors->has('day_id'))
<span class="invalid-feedback" role="alert">
<strong>{{ $errors->first('day_id') }}</strong>
</span>
@endif
</div>
<div class="field">
<label for="caption" class="col-md-4 col-form-label">Breakfast</label>
<input id="breakfast"
type="text"
class="form-control{{ $errors->has('breakfast') ? ' is-invalid' : '' }}"
name="breakfast"
value="{{ old('breakfast') }}"
autocomplete="name" autofocus>
@if ($errors->has('breakfast'))
<span class="invalid-feedback" role="alert">
<strong>{{ $errors->first('breakfast') }}</strong>
</span>
@endif
</div>
<div class="field">
<label for="caption" class="col-md-4 col-form-label">Lunch</label>
<input id="lunch"
type="text"
class="form-control{{ $errors->has('lunch') ? ' is-invalid' : '' }}"
name="lunch"
value="{{ old('lunch') }}"
autocomplete="name" autofocus>
@if ($errors->has('lunch'))
<span class="invalid-feedback" role="alert">
<strong>{{ $errors->first('lunch') }}</strong>
</span>
@endif
</div>
{{-- <div class="field">--}}
{{-- <label for="caption" class="col-md-4 col-form-label">Snack</label>--}}
{{-- <input id="snack"--}}
{{-- type="text"--}}
{{-- class="form-control{{ $errors->has('snack') ? ' is-invalid' : '' }}"--}}
{{-- name="snack[]"--}}
{{-- value="snack3{{ old('snack') }}"--}}
{{-- autocomplete="name" autofocus>--}}
{{-- @if ($errors->has('snack'))--}}
{{-- <span class="invalid-feedback" role="alert">--}}
{{-- <strong>{{ $errors->first('snack') }}</strong>--}}
{{-- </span>--}}
{{-- @endif--}}
{{-- </div>--}}
<div class="field">
<label for="caption" class="col-md-4 col-form-label">Dinner</label>
<input id="dinner"
type="text"
class="form-control{{ $errors->has('meal') ? ' is-invalid' : '' }}"
name="dinner"
value="{{ old('dinner') }}"
autocomplete="name" autofocus>
@if ($errors->has('dinner'))
<span class="invalid-feedback" role="alert">
<strong>{{ $errors->first('dinner') }}</strong>
</span>
@endif
</div>
<div class="field">
<label for="caption" class="col-md-4 col-form-label">Snack</label>
<input id="snack"
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 class="field">--}}
{{-- <label for="caption" class="col-md-4 col-form-label">Snack</label>--}}
{{-- <input id="snack"--}}
{{-- type="text"--}}
{{-- class="form-control{{ $errors->has('snack') ? ' is-invalid' : '' }}"--}}
{{-- name="snack[]"--}}
{{-- value="snack4{{ old('snack') }}"--}}
{{-- autocomplete="name" autofocus>--}}
{{-- @if ($errors->has('snack'))--}}
{{-- <span class="invalid-feedback" role="alert">--}}
{{-- <strong>{{ $errors->first('snack') }}</strong>--}}
{{-- </span>--}}
{{-- @endif--}}
{{-- </div>--}}
<div class="row pt-4">
<button class="btn btn-primary">Add New Post</button>
</div>
</form>
</div>
</div>
</div>
</form>
</div>
</body>
@endsection
select 日期下拉列表将得到更正。
MealPlanController.php
<?php
namespace App\Http\Controllers;
use App\MealPlan;
use Illuminate\Http\Request;
class MealPlanController extends Controller
{
public function __construct()
{
$this->middleware('auth', ['except' => ['show']]);
}
/**
* Display a listing of the resource.
*
* @return \Illuminate\Contracts\Foundation\Application|\Illuminate\Contracts\View\Factory|\Illuminate\Contracts\View\View|\Illuminate\Http\Response
*/
public function index()
{
$users = auth()->user()->following()->pluck('profiles.user_id');
$plans = MealPlan::whereIn('user_id', $users)->with('user')->latest()->paginate(5);
return view('meal.create.index', compact($plans));
}
/**
* Show the form for creating a new resource.
*
* @return \Illuminate\Contracts\Foundation\Application|\Illuminate\Contracts\View\Factory|\Illuminate\Contracts\View\View|\Illuminate\Http\Response
*/
public function create()
{
return view('meal.create');
}
/**
* Store a newly created resource in storage.
*
* @param \Illuminate\Http\Request $request
* @return \Illuminate\Contracts\Foundation\Application|\Illuminate\Http\RedirectResponse|\Illuminate\Http\Response|\Illuminate\Routing\Redirector
*/
public function store()
{
$data = request()->validate([
'day_id' => 'required',
'breakfast' => 'required',
'lunch' => 'required',
'dinner' => 'required',
'snack' => 'required',
]);
auth()->user()->plans()->create([
'day_id' => $data['day_id'],
'breakfast' => $data['breakfast'],
'lunch' => $data['lunch'],
'dinner' => $data['dinner'],
'snack' => $data['snack'],
]);
return redirect('/profile/' . auth()->user()->id);
}
/**
* Display the specified resource.
*
* @param \App\MealPlan $mealPlan
* @return \Illuminate\Http\Response
*/
public function show(MealPlan $mealPlan)
{
//
}
/**
* Show the form for editing the specified resource.
*
* @param \App\MealPlan $mealPlan
* @return \Illuminate\Http\Response
*/
public function edit(MealPlan $mealPlan)
{
//
}
/**
* Update the specified resource in storage.
*
* @param \Illuminate\Http\Request $request
* @param \App\MealPlan $mealPlan
* @return \Illuminate\Http\Response
*/
public function update(Request $request, MealPlan $mealPlan)
{
//
}
/**
* Remove the specified resource from storage.
*
* @param \App\MealPlan $mealPlan
* @return \Illuminate\Http\Response
*/
public function destroy(MealPlan $mealPlan)
{
//
}
}
每当尝试 post 它时,它只会刷新页面。请给我任何建议,我将不胜感激。如果我能得到进一步的建议,我什至可以传递我的电子邮件,否则很难在这里解释。
谢谢。
你还有很多需要改进的地方。
像这样改变你的一天table
Schema::create('days', function (Blueprint $table) {
$table->id();
$table->string('day');
$table->timestamps();
});
那么你应该像这样写一个播种器
public function run()
{
$days = [
['id' => 1, 'day' => 'Monday'],
['id' => 2, 'day' => 'Tuesday'],
['id' => 3, 'day' => 'Wednesday'],
['id' => 4, 'day' => 'Thursday'],
['id' => 5, 'day' => 'Friday'],
['id' => 6, 'day' => 'Saturday'],
['id' => 7, 'day' => 'Sunday'],
];
foreach ($days as $key => $day) {
DB::table('days')->insert($day);
}
}
这样就很容易在下拉列表中得到天数了。
您应该为 select 框添加名称属性。现在您有了选项的名称属性。错了。
零食是数组还是字符串?你有两个。由于您将其存储为文本。它不应该是一个数组。
商店终于要改了
public function store(Request $request)
{
$data = $request()->validate([
'day_id' => 'required',//Should come from the days table
'breakfast' => 'required',
'lunch' => 'required',
'dinner' => 'required',
'snack' => 'required',
]);
auth()->user()->plans()->create([
'day_id' => $request->get('day_id'),
'breakfast' => $request->get('breakfast'),
'lunch' => $request->get('lunch'),
'dinner' => $request->get('dinner'),
'snack' => $request->get('snack')
]);
return redirect('/profile/' . auth()->user()->id);
}
试试这个然后告诉我。
我正在尝试为用户建立一个基本的方式来制定一周的膳食计划。我已经尝试过几种方法,但由于这是我的第一个项目,我已经严重卡住了 atm。
所以下面我将展示我所采取的步骤并解释我的想法。
create_meal_plans_table.php
我喜欢让 table 尽可能简单,但这可能是我的失败。我最初将星期一到星期五的日子显示为单独的字符串,但它很乱,因此决定将其放在自己的日期 table.
中然后我添加了一个标题,这样他们就可以在用户个人资料中从下拉列表中选择一个特定的膳食计划,并且会显示出来。
剩下的不言而喻。
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
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('day_id');
$table->string('title');
$table->string('breakfast');
$table->string('lunch');
$table->string('dinner');
$table->string('snack');
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('meal_plans');
}
}
create_days_table
我不确定将日期保留为字符串是否正确。
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
class CreateDaysTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('days', function (Blueprint $table) {
$table->id();
$table->mediumText('monday'); //medium height text
$table->mediumText('tuesday'); //medium height text
$table->mediumText('wednesday'); //medium height text
$table->mediumText('thursday'); //medium height text
$table->mediumText('friday'); //medium height text
$table->mediumText('saturday'); //medium height text
$table->mediumText('sunday'); //medium height text
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('days');
}
}
Meal/create.blade.php
我不会对此进行解释,而是会截取它的外观并在下面显示。
@extends('layouts.app')
@section('content')
<head>
<style>
table .button {
background-color: transparent;
border: none;
color: black;
padding: 16px 32px;
text-align: center;
text-decoration: none;
display: inline-block;
font-size: 16px;
margin: 4px 2px;
/*transition-duration: 0.4s;*/
cursor: pointer;
}
table {
margin-left: auto;
margin-right: auto;
}
td {
width: 8rem;
height: 4rem;
border: 1px solid #ccc;
text-align: center;
}
th {
background: lightblue;
border-color: white;
}
body {
padding: 1rem;
}
</style>
</head>
<body>
<div class="container">
<form action="/mp" enctype="multipart/form-data" method="post">
@csrf
<div class="row">
<div class="col-8 offset-2">
<div class="row pb-3">
<h1>Add Meal Plan</h1>
</div>
<div class="row">
<table class="table" >
<tr>
<td><button class="button-1 button">MON</button></td>
<td><button class="button-2 button">TUE</button></td>
<td><button class="button-3 button">WED</button></td>
</tr>
<tr>
<td><button class="button-4 button">THURS</button></td>
<td><button class="button-5 button">FRI</button></td>
<td><button class="button-6 button">SAT</button></td>
</tr>
<tr>
<td></td>
<td><button class="button-7 button">SUN</button></td>
<td></td>
</tr>
</table>
</div>
<header>Enter Meals</header>
<div class="form-outer">
<form action="/mp" enctype="multipart/form-data" method="post">
@csrf
<div class="field">
<label for="caption" class="col-md-4 col-form-label">Day</label>
<select>
<option> --Select Tags--</option>
<option id="monday"
class="form-control"
name="monday">
Monday</option>
<option id="tuesday"
class="form-control"
name="tuesday">
Tuesday</option>
<option id="wednesday"
class="form-control"
name="wednesday">
Wednesday</option>
<option id="thursday"
class="form-control"
name="thursday">
Thursday</option>
<option id="friday"
class="form-control"
name="friday">
Friday</option>
<option id="saturday"
class="form-control"
name="saturday"
>Saturday</option>
<option id="sunday"
class="form-control"
name="sunday"
>Sunday</option>
</select>
@if ($errors->has('day_id'))
<span class="invalid-feedback" role="alert">
<strong>{{ $errors->first('day_id') }}</strong>
</span>
@endif
</div>
<div class="field">
<label for="caption" class="col-md-4 col-form-label">Breakfast</label>
<input id="breakfast"
type="text"
class="form-control{{ $errors->has('breakfast') ? ' is-invalid' : '' }}"
name="breakfast"
value="{{ old('breakfast') }}"
autocomplete="name" autofocus>
@if ($errors->has('breakfast'))
<span class="invalid-feedback" role="alert">
<strong>{{ $errors->first('breakfast') }}</strong>
</span>
@endif
</div>
<div class="field">
<label for="caption" class="col-md-4 col-form-label">Lunch</label>
<input id="lunch"
type="text"
class="form-control{{ $errors->has('lunch') ? ' is-invalid' : '' }}"
name="lunch"
value="{{ old('lunch') }}"
autocomplete="name" autofocus>
@if ($errors->has('lunch'))
<span class="invalid-feedback" role="alert">
<strong>{{ $errors->first('lunch') }}</strong>
</span>
@endif
</div>
{{-- <div class="field">--}}
{{-- <label for="caption" class="col-md-4 col-form-label">Snack</label>--}}
{{-- <input id="snack"--}}
{{-- type="text"--}}
{{-- class="form-control{{ $errors->has('snack') ? ' is-invalid' : '' }}"--}}
{{-- name="snack[]"--}}
{{-- value="snack3{{ old('snack') }}"--}}
{{-- autocomplete="name" autofocus>--}}
{{-- @if ($errors->has('snack'))--}}
{{-- <span class="invalid-feedback" role="alert">--}}
{{-- <strong>{{ $errors->first('snack') }}</strong>--}}
{{-- </span>--}}
{{-- @endif--}}
{{-- </div>--}}
<div class="field">
<label for="caption" class="col-md-4 col-form-label">Dinner</label>
<input id="dinner"
type="text"
class="form-control{{ $errors->has('meal') ? ' is-invalid' : '' }}"
name="dinner"
value="{{ old('dinner') }}"
autocomplete="name" autofocus>
@if ($errors->has('dinner'))
<span class="invalid-feedback" role="alert">
<strong>{{ $errors->first('dinner') }}</strong>
</span>
@endif
</div>
<div class="field">
<label for="caption" class="col-md-4 col-form-label">Snack</label>
<input id="snack"
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 class="field">--}}
{{-- <label for="caption" class="col-md-4 col-form-label">Snack</label>--}}
{{-- <input id="snack"--}}
{{-- type="text"--}}
{{-- class="form-control{{ $errors->has('snack') ? ' is-invalid' : '' }}"--}}
{{-- name="snack[]"--}}
{{-- value="snack4{{ old('snack') }}"--}}
{{-- autocomplete="name" autofocus>--}}
{{-- @if ($errors->has('snack'))--}}
{{-- <span class="invalid-feedback" role="alert">--}}
{{-- <strong>{{ $errors->first('snack') }}</strong>--}}
{{-- </span>--}}
{{-- @endif--}}
{{-- </div>--}}
<div class="row pt-4">
<button class="btn btn-primary">Add New Post</button>
</div>
</form>
</div>
</div>
</div>
</form>
</div>
</body>
@endsection
select 日期下拉列表将得到更正。
MealPlanController.php
<?php
namespace App\Http\Controllers;
use App\MealPlan;
use Illuminate\Http\Request;
class MealPlanController extends Controller
{
public function __construct()
{
$this->middleware('auth', ['except' => ['show']]);
}
/**
* Display a listing of the resource.
*
* @return \Illuminate\Contracts\Foundation\Application|\Illuminate\Contracts\View\Factory|\Illuminate\Contracts\View\View|\Illuminate\Http\Response
*/
public function index()
{
$users = auth()->user()->following()->pluck('profiles.user_id');
$plans = MealPlan::whereIn('user_id', $users)->with('user')->latest()->paginate(5);
return view('meal.create.index', compact($plans));
}
/**
* Show the form for creating a new resource.
*
* @return \Illuminate\Contracts\Foundation\Application|\Illuminate\Contracts\View\Factory|\Illuminate\Contracts\View\View|\Illuminate\Http\Response
*/
public function create()
{
return view('meal.create');
}
/**
* Store a newly created resource in storage.
*
* @param \Illuminate\Http\Request $request
* @return \Illuminate\Contracts\Foundation\Application|\Illuminate\Http\RedirectResponse|\Illuminate\Http\Response|\Illuminate\Routing\Redirector
*/
public function store()
{
$data = request()->validate([
'day_id' => 'required',
'breakfast' => 'required',
'lunch' => 'required',
'dinner' => 'required',
'snack' => 'required',
]);
auth()->user()->plans()->create([
'day_id' => $data['day_id'],
'breakfast' => $data['breakfast'],
'lunch' => $data['lunch'],
'dinner' => $data['dinner'],
'snack' => $data['snack'],
]);
return redirect('/profile/' . auth()->user()->id);
}
/**
* Display the specified resource.
*
* @param \App\MealPlan $mealPlan
* @return \Illuminate\Http\Response
*/
public function show(MealPlan $mealPlan)
{
//
}
/**
* Show the form for editing the specified resource.
*
* @param \App\MealPlan $mealPlan
* @return \Illuminate\Http\Response
*/
public function edit(MealPlan $mealPlan)
{
//
}
/**
* Update the specified resource in storage.
*
* @param \Illuminate\Http\Request $request
* @param \App\MealPlan $mealPlan
* @return \Illuminate\Http\Response
*/
public function update(Request $request, MealPlan $mealPlan)
{
//
}
/**
* Remove the specified resource from storage.
*
* @param \App\MealPlan $mealPlan
* @return \Illuminate\Http\Response
*/
public function destroy(MealPlan $mealPlan)
{
//
}
}
每当尝试 post 它时,它只会刷新页面。请给我任何建议,我将不胜感激。如果我能得到进一步的建议,我什至可以传递我的电子邮件,否则很难在这里解释。
谢谢。
你还有很多需要改进的地方。
像这样改变你的一天table
Schema::create('days', function (Blueprint $table) {
$table->id();
$table->string('day');
$table->timestamps();
});
那么你应该像这样写一个播种器
public function run()
{
$days = [
['id' => 1, 'day' => 'Monday'],
['id' => 2, 'day' => 'Tuesday'],
['id' => 3, 'day' => 'Wednesday'],
['id' => 4, 'day' => 'Thursday'],
['id' => 5, 'day' => 'Friday'],
['id' => 6, 'day' => 'Saturday'],
['id' => 7, 'day' => 'Sunday'],
];
foreach ($days as $key => $day) {
DB::table('days')->insert($day);
}
}
这样就很容易在下拉列表中得到天数了。
您应该为 select 框添加名称属性。现在您有了选项的名称属性。错了。
零食是数组还是字符串?你有两个。由于您将其存储为文本。它不应该是一个数组。
商店终于要改了
public function store(Request $request)
{
$data = $request()->validate([
'day_id' => 'required',//Should come from the days table
'breakfast' => 'required',
'lunch' => 'required',
'dinner' => 'required',
'snack' => 'required',
]);
auth()->user()->plans()->create([
'day_id' => $request->get('day_id'),
'breakfast' => $request->get('breakfast'),
'lunch' => $request->get('lunch'),
'dinner' => $request->get('dinner'),
'snack' => $request->get('snack')
]);
return redirect('/profile/' . auth()->user()->id);
}
试试这个然后告诉我。