我目前正在 Laravel 10 中开发一个电子商务项目。在类别部分的管理仪表板中,我设置了两列,一列用于类别图像,另一列用于类别横幅图像。
我的问题出现在我的视图成功显示类别图像,但不显示横幅图像。以下是控制器的代码:
<?php
namespace App\Http\Controllers\admin;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use App\Models\Category;
use Illuminate\Support\Facades\File;
use App\Models\Product;
class CategoryController extends Controller
{
/**
* Display a listing of the resource.
*/
public function index()
{
$category = Category::orderBy('created_at','DESC')->get();
//dd($category);
return view('admin.category.index',compact('category'));
}
/**
* Show the form for creating a new resource.
*/
public function create()
{
return view('admin.category.create');
}
/**
* Store a newly created resource in storage.
*/
public function store(Request $request)
{
$request->validate([
'image' => 'nullable|mimes:png,jpg,jpeg,webp',
'name' => 'required|unique:categories',
'slug' => 'required',
'status' => 'required',
'categorybanner'=>'nullable|mimes:png,jpg,jpeg,webp'
]);
if($request->has('image'))
{
$file = $request->file('image');
$extension=$file->getClientOriginalExtension();
$filename=time().'.'.$extension;
$path='uploads/category/';
$file->move($path,$filename);
}
if($request->has('categorybanner'))
{
$file2=$request->file('categorybanner');
$extension2=$file2->getClientOriginalExtension();
$filename2=time().'.'.$extension2;
$path2='uploads/categorybanner/';
$file2->move($path2,$filename2);
}
Category::create([
'image'=>$path.$filename,
'name'=>$request->name,
'slug'=>$request->slug,
'status'=>$request->status,
'categorybanner'=>$path2.$filename2
]);
return redirect()->route('category.index')->with('success','New Category defined');
}
/**
* Display the specified resource.
*/
public function show(string $id)
{
$category = Category::findOrFail($id);
return view('admin.category.show',compact('category'));
}
/**
* Show the form for editing the specified resource.
*/
public function edit(string $id)
{
$category = Category::findOrFail($id);
return view('admin.category.edit',compact('category'));
}
/**
* Update the specified resource in storage.
*/
public function update(Request $request, string $id)
{
$request->validate([
'image' => 'nullable|mimes:png,jpg,jpeg,webp',
'name' => 'required',
'slug' => 'required',
'status' => 'required',
'categorybanner'=>'nullable|mimes:png,jpg,jpeg,webp'
]);
$category = Category::findOrFail($id);
if($request->has('image'))
{
$file = $request->file('image');
$extension=$file->getClientOriginalExtension();
$filename=time().'.'.$extension;
$path='uploads/category/';
$file->move($path,$filename);
if(File::exists($category->image))
{
File::delete($category->image);
}
$category->update([
'image'=>$path.$filename,
'name'=>$request->name,
'slug'=>$request->slug,
'status'=>$request->status,
]);
}
if($request->has('categorybanner'))
{
$file2=$request->file('categorybanner');
$extension2=$file2->getClientOriginalExtension();
$filename2=time().'.'.$extension2;
$path2='uploads/categorybanner/';
$file2->move($path2,$filename2);
if(File::exists($category->categorybanner))
{
File::delete($category->categorybanner);
}
$category->update([
'name'=>$request->name,
'slug'=>$request->slug,
'status'=>$request->status,
'categorybanner'=>$path2.$filename2
]);
}
$category->update([
'name'=>$request->name,
'slug'=>$request->slug,
'status'=>$request->status
]);
return redirect()->route('category.index')->with('success','Category updated successfully');
}
/**
* Remove the specified resource from storage.
*/
public function destroy(string $id)
{
$category=Category::findOrFail($id);
$product = Product::where('category_id', $category['id'])->first();
if(!$product)
{
if(File::exists($category->image))
{
File::delete($category->image);
}
if(File::exists($category->categorybanner))
{
File::delete($category->categorybanner);
}
$category->delete();
return redirect()->route('category.index')->with('success','Category deleted successfully');
}
else
{
return redirect()->route('category.index')->with('message','Cannot delete Categories with existing products!');
}
}
}
以下是刀片文件索引部分的代码片段,应该显示图像:
@if($category->count()>0)
@foreach($category as $ct)
<tr>
<td class="align-middle">{{$loop->iteration}}</td>
<td>
<img src="{{asset($ct->image)}}" style="width: 70px; height: 70px;" alt="img" />
</td>
<td class="align-middle">{{ $ct->name }}</td>
<td class="align-middle">{{ $ct->slug }}</td>
<td>
<img src="{{asset('$ct->categorybanner')}}" style="width: 70px; height: 70px;" alt="categorybanner" />
</td>
<td>
@if($ct->status==1)
<svg class="text-success-500 h-6 w-6 text-success" xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" stroke-width="2" stroke="currentColor" aria-hidden="true">
<path stroke-linecap="round" stroke-linejoin="round" d="M9 12l2 2 4-4m6 2a9 9 0 11-18 0 9 9 0 0118 0z"></path>
@else
<svg class="text-danger h-6 w-6" xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" stroke-width="2" stroke="currentColor" aria-hidden="true">
<path stroke-linecap="round" stroke-linejoin="round" d="M10 14l2-2m0 0l2-2m-2 2l-2-2m2 2l2 2m7-2a9 9 0 11-18 0 9 9 0 0118 0z"></path>
@endif
</td>
<td class="align-middle">
<div class = "btn-group" role="group" aria-label="Basic Example">
<a href="{{route('category.show',$ct->id)}}" type="button" class="btn btn-secondary">Details</a>
<a href="{{route('category.edit',$ct->id)}}" type="button" class="btn btn-warning">Edit</a>
<form method="POST" action="{{ route('category.destroy', $ct->id) }}" type="button" class="btn btn-danger p-0" onsubmit="return confirm('Delete?')">
@csrf
@method('delete')
<button class="btn btn-danger m-0">Delete</button>
</form>
</div>
</td>
</tr>
@endforeach
@else
<tr>
<td class="text-center" colspan="5">Category Not Found</td>
</tr>
@endif
</tbody>
</table>
</div>
我解决了问题,现在可以显示图像了。单引号 '' 存在问题,我必须从视图文件中删除它们。