我正在使用Laravel 5.6,在我的应用中,我具有显示带有编辑和删除按钮index.blade.php的刀片的类别
<div class="col-md-9">
<div class="float-right">
<a href="{{route('categories.form')}}" class="btn btn-primary">New</a>
</div>
<h1 style="font-size: 2.2rem">Vehicle Categories</h1>
<hr/>
<!--add here-->
<div class="row">
@foreach($images as $image)
<div class="col-md-4 col-lg-3" style="margin-bottom: 20px">
<div class="card">
<img class="card-img-top"
src="{{url($image->categoryimage? 'images/'.$image->categoryimage:'images/noimage.jpg')}}"
alt="{{$image->name}}" width="100%" height="180px"/>
<div class="card-body">
<h6 class="card-title text-center">{{ucwords($image->categoryname)}}</h6>
</div>
<ul class="list-group list-group-flush">
<li class="list-group-item">
<form id="frm_{{$image->id}}"
action="{{url('delete/'.$image->id)}}"
method="post" style="padding-bottom: 0px;margin-bottom: 0px">
<div class="row">
<div class="col-sm-6">
<a href="javascript:if(confirm('Are you sure want to delete?')) $('#frm_{{$image->id}}').submit()"
class="btn btn-danger btn-sm btn-block">Delete</a>
</div>
<div class="col-sm-6">
<a href="{{url('category/update/'.$image->id)}}"
class="btn btn-primary btn-sm btn-block">Edit</a>
</div>
<input type="hidden" name="_method" value="delete"/>
{{csrf_field()}}
</div>
</form>
</li>
</ul>
</div>
</div>
@endforeach
我的编辑表单在form.blade.php中,具有新类别添加表单和编辑表单,
<div class="col-md-9">
<h1>{{isset($image)?'Edit':'New'}} Category</h1>
<hr/>
@if(isset($image))
<form method="PUT" action="{{url('updateform')}}" enctype="multipart/form-data">
<input type="hidden" name="_method" value="put">
@else
<form method="POST" action="http://localhost:8000/create" accept-charset="UTF-8" enctype="multipart/form-data"><input name="_token" type="hidden" value="1LLYc0D1spmVSFMboLDjGM9MR4O5APVwng7giejx">
{{csrf_field()}}
@endif
<div class="form-group row required">
<label for="description" class="col-form-label col-md-3 col-lg-2">Description</label>
<div class="col-md-8">
<input class="form-control" autofocus placeholder="Description" name="description" type="text" id="description">
</div>
</div>
<div class="form-group row">
<label for="image" class="col-form-label col-md-3">Image</label>
<div class="col-md-5">
<img id="preview"
src="{{asset((isset($image) && $image->categoryimage!='')?'images/'.$image->categoryimage:'images/noimage.png')}}"
height="200px" width="200px"/>
<input class="form-control" style="display:none" name="image" type="file" id="image" name="_token" value="{{ csrf_token() }}">
<br/>
<a href="javascript:changeProfile();">Add Image</a> |
<a style="color: red" href="javascript:removeImage()">Remove</a>
<input type="hidden" style="display: none" value="0" name="remove" id="remove">
</div>
</div>
我的控制器,
public function index(Request $request)
{
$images = Category::orderBy('created_at', 'ASC')->paginate(8);
return view('categories.index', compact('images'));
}
public function update(Request $request, $id)
{
if ($request->isMethod('get'))
return view('categories.form', ['categoryimage' => Category::find($id)]);
$image = Category::find($id);
if ($request->hasFile('image')) {
$dir = 'images/';
if ($image->image != '' && File::exists($dir . $image->image))
File::delete($dir . $image->image);
$extension = strtolower($request->file('image')->getClientOriginalExtension());
$fileName = str_random() . '.' . $extension;
$request->file('image')->move($dir, $fileName);
$image->categoryimage = $fileName;
} elseif ($request->remove == 1 && File::exists('images/' . $image->image)) {
File::delete('images/' . $image->post_image);
$image->categoryimage = null;
}
$image->categoryname = $request->description;
$image->save();
return redirect()->route('categories.index');
}
和路线是
Route::match(['get', 'put'], 'category/update/{id}', 'CategoryController@update');
现在,我的问题是,当我单击“新建”按钮以添加新类别时,它运行良好。但是,当我单击“编辑”按钮时,它没有显示编辑表单,而是一直显示新的添加类别表单。就我而言,我认为这不需要
<h1>{{isset($image)?'Edit':'New'}} Category</h1>
($ image)变量以显示“编辑”表单。如何解决此问题?