Laravel 6.x - 无法在控制器方法中更新表单(Nested-Child)
Laravel 6.x - Cannot update form in Controller method(Nested-Child)
我是 Laravel 的新人。目前我遇到了一个问题,我被困在 从所有 child 更新详细信息的过程中,但在更新 parent 的详细信息方面还不错。所以在这里我将展示 child 之一,即 Staff(Process as Parent)
1) StaffController.php(更新方法)
public function update($processes_id, Request $request, Staff $staff)
{
// $staff->update($request->all());
// return redirect()->route('processes.staffs.index', $processes_id);
$request->validate([
'staffName' => 'required',
'staffDept' => 'required',
'staffSection' => 'required',
]);
$staff->update($request->all());
// $staff = Process::find($id);
// $staff->staffName = $request->input('staffName');
// $staff->staffDept = $request->input('staffDept');
// $staff->staffSection = $request->input('staffSection');
// $process->save();
return redirect()->route('processes.staffs.index')
->with('success','Asset updated successfully.');
}
我试过注释代码,输入新的详细信息时仍然没有任何变化。
2) edit.blade.php
@extends('layouts.app')
@section('content')
<h1>Edit Staff</h1>
<!-- FORM -->
{{-- {!! Form::open(['action' => ['ProcessesController@update', [$staff->id, $staff->processes_id]], 'method' => 'POST', 'enctype' => 'multipart/form-data']) !!}
<div class="form-group">
{{Form::label('staffName', 'staffName:')}}
{{Form::text('staffName', $staff->staffName, ['class' => 'form-control'])}}
</div>
<div class="form-group">
{{Form::label('staffDept', 'staffDept:')}}
{{Form::text('staffDept', $staff->staffDept, ['class' => 'form-control'])}}
</div>
<div class="form-group">
{{Form::label('staffSection', 'staffSection:')}}
{{Form::text('staffSection', $staff->staffSection, ['class' => 'form-control'])}}
</div>
{{Form::submit('Submit', ['class'=>'btn btn-primary'])}}
<a href="/processes" class="btn btn-primary">Back</a>
</div>
{{Form::hidden('_method', 'PUT')}}
{!! Form::close() !!} --}}
<form action="{{ route('processes.staffs.update', [$staff->processes_id, $staff->id]) }}" method="POST">
@csrf
@method('PUT')
<div class="row">
<div class="col-xs-12 col-sm-12 col-md-12">
<div class="form-group">
<strong>Staff Name:</strong>
<input type="text" name="name" value="{{ $staff->staffName }}" class="form-control" placeholder="Name">
</div>
</div>
<div class="col-xs-12 col-sm-12 col-md-12">
<div class="form-group">
<strong>Staff Department:</strong>
<input type="text" name="name" value="{{ $staff->staffDept }}" class="form-control" placeholder="Name">
</div>
</div>
<div class="col-xs-12 col-sm-12 col-md-12">
<div class="form-group">
<strong>Staff Section:</strong>
<input type="text" name="name" value="{{ $staff->staffSection }}" class="form-control" placeholder="Name">
</div>
</div>
<div class="col-xs-12 col-sm-12 col-md-12 text-left">
<button type="submit" class="btn btn-primary">Submit</button>
<a href="/processes" class="btn btn-primary">Back</a>
</div>
</div>
</form>
@endsection
也尝试了上面的注释形式,它给了我 Array to Conversion Error.
3) index.blade.php(编辑 link 在此 blade 文件中)
@extends('layouts.app')
@section('content')
<h1>Staffs</h1>
@if(count($staffs) > 0)
<table class="table table-bordered">
<tr>
<th>No.</th>
<th>Name</th>
<th>Department</th>
<th>Section</th>
<th width="280px">Action</th>
</tr>
@php $i = 0 @endphp
@foreach ($staffs as $staff)
<tr>
<td>{{ ++$i }}</td>
<td>{{ $staff->staffName }}</td>
<td>{{ $staff->staffDept }}</td>
<td>{{ $staff->staffSection }}</td>
<td>
<a class="btn btn-info" href="{{ route('processes.staffs.show',[$staff->processes_id, $staff->id]) }}">Show</a>
<a class="btn btn-primary" href="{{ route('processes.staffs.edit',[$staff->processes_id, $staff->id]) }}">Edit</a>
@csrf
{{-- @method('DELETE')
<button type="submit" class="btn btn-danger">Delete</button> --}}
<form action="{{ route('processes.staffs.destroy', [$staff->processes_id, $staff->id]) }}" method="POST">
<input type="hidden" name="_method" value="DELETE">
<input type="hidden" name="_token" value="{{ csrf_token() }}">
<input type="submit" class="btn btn-xs btn-danger" value="Delete">
</form>
</td>
</tr>
@endforeach
</table>
{{-- {{$staffs->links()}} --}}
@else
<p>No staffs found!</p>
@endif
<a href="{{ route("processes.staffs.create", $processes_id) }}" class="btn btn-primary">Add New Staff</a>
<a href="/processes" class="btn btn-primary">Back</a>
@endsection
4) web.php(路由文件)
//Asset
Route::resource('processes','ProcessesController');
// Staff
Route::resource('processes.staffs','StaffController');
Route::PUT('/processes/{process}/staffs/{staff}','StaffController@update');
非常欢迎和感谢任何建议和回答。
我从朋友那里找到了解决方法,就是edit.blade.php更新表单中的愚蠢错误。
(name="name") 应该根据其在数据库中的变量名进行更改 table
<input type="text" name="name" value="{{ $staff->staffName }}" class="form-control" placeholder="Name">
所以,下面是正确的:
<input type="text" name="staffName" value="{{ $staff->staffName }}" class="form-control" placeholder="Name">
暂时感谢,它可以工作并且可以更新详细信息。
我是 Laravel 的新人。目前我遇到了一个问题,我被困在 从所有 child 更新详细信息的过程中,但在更新 parent 的详细信息方面还不错。所以在这里我将展示 child 之一,即 Staff(Process as Parent)
1) StaffController.php(更新方法)
public function update($processes_id, Request $request, Staff $staff)
{
// $staff->update($request->all());
// return redirect()->route('processes.staffs.index', $processes_id);
$request->validate([
'staffName' => 'required',
'staffDept' => 'required',
'staffSection' => 'required',
]);
$staff->update($request->all());
// $staff = Process::find($id);
// $staff->staffName = $request->input('staffName');
// $staff->staffDept = $request->input('staffDept');
// $staff->staffSection = $request->input('staffSection');
// $process->save();
return redirect()->route('processes.staffs.index')
->with('success','Asset updated successfully.');
}
我试过注释代码,输入新的详细信息时仍然没有任何变化。
2) edit.blade.php
@extends('layouts.app')
@section('content')
<h1>Edit Staff</h1>
<!-- FORM -->
{{-- {!! Form::open(['action' => ['ProcessesController@update', [$staff->id, $staff->processes_id]], 'method' => 'POST', 'enctype' => 'multipart/form-data']) !!}
<div class="form-group">
{{Form::label('staffName', 'staffName:')}}
{{Form::text('staffName', $staff->staffName, ['class' => 'form-control'])}}
</div>
<div class="form-group">
{{Form::label('staffDept', 'staffDept:')}}
{{Form::text('staffDept', $staff->staffDept, ['class' => 'form-control'])}}
</div>
<div class="form-group">
{{Form::label('staffSection', 'staffSection:')}}
{{Form::text('staffSection', $staff->staffSection, ['class' => 'form-control'])}}
</div>
{{Form::submit('Submit', ['class'=>'btn btn-primary'])}}
<a href="/processes" class="btn btn-primary">Back</a>
</div>
{{Form::hidden('_method', 'PUT')}}
{!! Form::close() !!} --}}
<form action="{{ route('processes.staffs.update', [$staff->processes_id, $staff->id]) }}" method="POST">
@csrf
@method('PUT')
<div class="row">
<div class="col-xs-12 col-sm-12 col-md-12">
<div class="form-group">
<strong>Staff Name:</strong>
<input type="text" name="name" value="{{ $staff->staffName }}" class="form-control" placeholder="Name">
</div>
</div>
<div class="col-xs-12 col-sm-12 col-md-12">
<div class="form-group">
<strong>Staff Department:</strong>
<input type="text" name="name" value="{{ $staff->staffDept }}" class="form-control" placeholder="Name">
</div>
</div>
<div class="col-xs-12 col-sm-12 col-md-12">
<div class="form-group">
<strong>Staff Section:</strong>
<input type="text" name="name" value="{{ $staff->staffSection }}" class="form-control" placeholder="Name">
</div>
</div>
<div class="col-xs-12 col-sm-12 col-md-12 text-left">
<button type="submit" class="btn btn-primary">Submit</button>
<a href="/processes" class="btn btn-primary">Back</a>
</div>
</div>
</form>
@endsection
也尝试了上面的注释形式,它给了我 Array to Conversion Error.
3) index.blade.php(编辑 link 在此 blade 文件中)
@extends('layouts.app')
@section('content')
<h1>Staffs</h1>
@if(count($staffs) > 0)
<table class="table table-bordered">
<tr>
<th>No.</th>
<th>Name</th>
<th>Department</th>
<th>Section</th>
<th width="280px">Action</th>
</tr>
@php $i = 0 @endphp
@foreach ($staffs as $staff)
<tr>
<td>{{ ++$i }}</td>
<td>{{ $staff->staffName }}</td>
<td>{{ $staff->staffDept }}</td>
<td>{{ $staff->staffSection }}</td>
<td>
<a class="btn btn-info" href="{{ route('processes.staffs.show',[$staff->processes_id, $staff->id]) }}">Show</a>
<a class="btn btn-primary" href="{{ route('processes.staffs.edit',[$staff->processes_id, $staff->id]) }}">Edit</a>
@csrf
{{-- @method('DELETE')
<button type="submit" class="btn btn-danger">Delete</button> --}}
<form action="{{ route('processes.staffs.destroy', [$staff->processes_id, $staff->id]) }}" method="POST">
<input type="hidden" name="_method" value="DELETE">
<input type="hidden" name="_token" value="{{ csrf_token() }}">
<input type="submit" class="btn btn-xs btn-danger" value="Delete">
</form>
</td>
</tr>
@endforeach
</table>
{{-- {{$staffs->links()}} --}}
@else
<p>No staffs found!</p>
@endif
<a href="{{ route("processes.staffs.create", $processes_id) }}" class="btn btn-primary">Add New Staff</a>
<a href="/processes" class="btn btn-primary">Back</a>
@endsection
4) web.php(路由文件)
//Asset
Route::resource('processes','ProcessesController');
// Staff
Route::resource('processes.staffs','StaffController');
Route::PUT('/processes/{process}/staffs/{staff}','StaffController@update');
非常欢迎和感谢任何建议和回答。
我从朋友那里找到了解决方法,就是edit.blade.php更新表单中的愚蠢错误。
(name="name") 应该根据其在数据库中的变量名进行更改 table
<input type="text" name="name" value="{{ $staff->staffName }}" class="form-control" placeholder="Name">
所以,下面是正确的:
<input type="text" name="staffName" value="{{ $staff->staffName }}" class="form-control" placeholder="Name">
暂时感谢,它可以工作并且可以更新详细信息。