I am currently working on an ecommerce project in laravel 10. In the admin dashboard in my categories section, I have set two columns, one for category images and the other column for category banner images.
My problem arises where my view successfully displays category images, but does not display the banner image. Following is the code for controller:
<?php
namespace AppHttpControllersadmin;
use AppHttpControllersController;
use IlluminateHttpRequest;
use AppModelsCategory;
use IlluminateSupportFacadesFile;
use AppModelsProduct;
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!');
}
}
}
Follwing is the code snippet of the index part of the blade file that is supposed to display the image:
@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>
2
Answers
I solved the issue and the images are being displayed now. There was an issue with single quotes '' and I had to removed those from the view file.
which img you want to display? is it
right?