Laravel Image Uploading with filepath but not showing in view

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:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code> <?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!');
}
}
}
</code>
<code> <?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!'); } } } </code>
    <?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:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>@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>
</code>
<code>@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> </code>
@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>

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật