0
こんにちは私のフォームからのデータの編集や更新に問題があります。私はすでにフォームからデータを取得しようとしましたが、編集して更新しようとすると別のデータが作成されます。ありがとう!Laravel 5.2のフォームからデータを更新する方法
Controller:
public function registerPackage()
{
$activity_packages = Chap_activity_packages::all();
return view('admin.registerPackage',compact('activity_packages'));
}
public function savePackage(Request $request)
{
$this->validate($request,[
'chap_activity_packages_name'=>'required|Min:4|unique:chap_activity_packa ges',
'chap_activity_packages_price'=>'required|numeric'
]);
$values = $request->all();
Chap_activity_packages::create($values);
return view('admin.registerPackage');
}
public function editPackage($id)
{
$act = Chap_activity_packages::find($id);
$activity_packages=Chap_activity_packages::all();
return view('admin.registerPackage',compact('act','activity_packages'));
}
<h4 class="page-header">Packages Management</h4>
<div class="row">
<div class="col-xs-5">
<div class="panel panel-success">
<div class="panel-heading">
<h3 class="panel-title">Register New Package</h3>
@if($errors->any())
<div class="alert alert-danger">
@foreach($errors->all() as $error)
<div>{{ $error }}</div>
@endforeach
</div>
@endif
@if(Session::has('flash_message'))
<div class="alert alert-success">
{{ Session::get('flash_message') }}
</div>
@endif
</div>
<div class="panel-body">
{{ Form::open(array('url' => 'admin/registerPackage')) }}
<div class="form-group">
{{ Form::label('chap_name','Package Name:') }}
{{ Form::text('chap_activity_packages_name',isset($act)? $act->chap_activity_packages_name:null,['class' => 'form-control', 'placeholder' => 'Enter package name']) }}
</div>
<div class="form-group">
{{ Form::label('chap_price','Package Price:') }}
{{ Form::text('chap_activity_packages_price',isset($act)? $act->chap_activity_packages_price:null,['class' => 'form-control', 'placeholder' => 'Enter package price']) }}
</div>
<div class="form-group">
{{ Form::submit('Register Package',['class' => 'btn btn-success btn-block']) }}
</div>
{{ Form::close() }}
</div>
</div>
</div>
</div>
@stop
@section('content2')
<div class="panel panel-success">
<div class="panel-heading">
<h4 class="panel-title">Available Packages</h4>
</div>
<div class="panel-body">
<table class="table table-striped table-hover">
<thead>
<tr>
<th>Package Name</th>
<th>Package Price</th>
<th>Action</th>
</tr>
</thead>
<tbody>
@foreach($activity_packages as $activity_package)
<tr>
<td> {{ $activity_package->chap_activity_packages_name }} </td>
<td> {{ $activity_package->chap_activity_packages_price }} </td>
<td>
<a href='{{ url("admin/editPackage/$activity_package->id") }}'><span class="glyphicon glyphicon-pencil"></span></a>
<a href="#"><span class="glyphicon glyphicon-trash"></span></a>
</td>
</tr>
@endforeach
</tbody>
</table>
</div>
</div>
@stop
これはコントローラとビューの両方のコードです。何が間違っているか教えてください。どうもありがとう!結束ルートモデルについて
// YourController.php, use implicit route model binding
public function update(App\Chap_activity_packages $model)
{
// do your validation here
// $this->validate(blahblahblah...)
// ...
$model->update(request()->all());
return redirect()->back()->with('msg', 'Update success.');
}
// routes.php
Route::put('your/route/to/model/{model}', '[email protected]');
// yourTemplate.blade.php
// remember to use method spoofing
{{ Form::open(['url' => 'your/route/to/model/' . $model->id, 'method' => 'put']) }}
情報はhereを見つけることができます:
コードを適切に区切ってフォーマットすると、pplがそれを読みづらい – tam5