【发布时间】:2021-05-30 02:27:47
【问题描述】:
我想更新 MySQL 表中包含项目数据的某些特定列。名为“项目”的表有很多列,但我希望用户只更新几列。我已经为它编写了一些代码,但是当用户提交表单时,表中的数据不会更新。我对 Laravel 很陌生。请帮忙。
注意:我已将“start_date”列为字符串,但我可以稍后更改。现在我只想更新我的数据库中的数据。
在我的控制器中:
/**
* Show the form for editing the specified Project.
*
*/
public function edit(Project $project, $id)
{
$project = Project::findOrFail($id);
return view('client_panel.projects.edit',compact('project'));
}
/**
* Update the specified Project in storage.
*
* @param \Illuminate\Http\Request $request
* @param \App\Project $project
* @return \Illuminate\Http\Response
*/
public function update(Request $request, Project $project)
{
$request->validate([
'name' => 'required|string|max:255',
'description' => 'required|string|max:255',
'start_date' => 'required|date',
'start_time' => 'required|string|max:10',
]);
$project->update($request->all());
return redirect()->back()->with('message', 'You have successfully updated your Job Card');
}
}
我在 Blade View 中的编辑表单:
<form method="POST" action="{{ route('client-jobcard.update',$project->id) }}">
@csrf
@method('PATCH')
<div class="form-group row">
<div class="col-sm-8">
<label for="name">{{ __('Job Card Name') }}</label><span class="required">*</span>
<input id="name" type="text" class="form-control{{ $errors->has('name') ? ' is-invalid' : '' }}" name="name" value="{{ $project->name }}" required autofocus>
@if ($errors->has('name'))
<span class="invalid-feedback">
<strong>{{ $errors->first('name') }}</strong>
</span>
@endif
</div>
</div>
<div class="form-group row">
<div class="col-sm-8">
<label for="description">{{ __('Job Card Description') }}</label><span class="required">*</span>
<textarea class="form-control{{ $errors->has('description') ? ' is-invalid' : '' }}" id="description" rows="4" style="height: 150px;" name="description" required autofocus>{{ $project->description }}</textarea>
@if ($errors->has('description'))
<span class="invalid-feedback">
<strong>{{ $errors->first('description') }}</strong>
</span>
@endif
</div>
</div>
<div class="form-group row">
<div class="col-sm-4">
<label for="start_date">Start Date</label><span class="required">*</span>
<input type="date" class="form-control" id="start_date" name="start_date" value="{{ $project->start_date }}" required>
</div>
<div class="col-sm-4">
<label for="submission_date">Start Time</label><span class="required">*</span>
<input type="text" class="form-control" id="start_time" name="start_time" value="{{ $project->start_time }}" required>
</div>
</div>
<div class="form-group row">
<div class="col-sm-4">
<button type="submit" class="btn btn-primary">
{{ __('Submit') }}
</button>
</div>
</div>
</form>
路线:
Route::get('client-jobcard/create', 'ProjectController@create')->name('client-jobcard.create');
Route::post('client-jobcard', 'ProjectController@store')->name('client-jobcard.store');
Route::get('client-jobcard/edit/{id}', 'ProjectController@edit')->name('client-jobcard.edit');
Route::patch('client-jobcard/{id}', 'ProjectController@update')->name('client-jobcard.update');
迁移文件中的数据库列:
Schema::create('projects', function (Blueprint $table) {
$table->increments('id');
$table->string('name', 170);
$table->unsignedInteger('client_id')->nullable();
$table->unsignedInteger('fundi_id')->nullable();
$table->text('description')->nullable();
$table->integer('price')->nullable();
$table->integer('currency')->nullable();
$table->string('prefix', 170)->default('JOB');
$table->string('color')->default('#2E73BA');
$table->date('start_date')->nullable();
$table->string('start_time')->nullable();
$table->unsignedInteger('created_by')->nullable();
$table->unsignedInteger('deleted_by')->nullable();
$table->timestamps();
$table->softDeletes();
【问题讨论】: