Search code examples
file-uploadlaravel-9

Laravel 9-when i upload images they are stored in the database but i cant find them in the storage folder


when i upload an image it's added successfully to the database but when i try to display it it doesn't show, i checked the storage folder and found that the images aren't being saved, tho i tried php artisan storage:link , it does create the storage folder in public but when when i upload an image it doesn't get added there

CategoryController.php

public function store(Request $request, Category $category)
    {  
        
        $request->validate([

            'name'=>'required',
            'slug'=>'required',
            'description'=>'required',
            'meta_title',
            'meta_description',
            'meta_keywords'
        ]);

        if($request->hasFile('image')){
            $filename = $request->image->getClientOriginalName();
            $request->image->storeAs('images',$filename,'public');
        }


        Category::create($request->all());

        return redirect()->route('category.index')->with('success', 'Category was added successfully !');
    }

index.blade.php


  <table class="table table-striped " >
    
        <tr>
          <th>#</th>
          <th>Name</th>
          <th>Slug</th>
          <th>Description</th>
          <th>Status</th>
          <th>Popular</th>
          <th>Image</th>
          <th>Meta Title</th>
          <th>Meta Description</th>
          <th>Meta Keywords</th>
          <th width="200px">Actions</th>
        </tr>

    
        @foreach($category as $category)
        <tr>
            <th >{{$category->id}}</th>
            <td>{{$category->name}} </td>
            <td>{{$category->slug   }} </td>
            <td>{{$category->description}}</td>
            <td>{{$category->status}}</td>
            <td>{{$category->popular}}</td>
            <td> <img class="rounded-circle mt-4" src="{{asset('/storage/images/'.$category->image)}}"style="width: 50px;height: 50px; "></td>
            <td>{{$category->meta_title}}</td>
            <td>{{$category->meta_description}}</td>
            <td>{{$category->meta_keywords}}</td>

            <td >

                <form action="{{ route('category.destroy', $category->id)}}" method="POST">

                    <a href="{{ route('category.edit',$category->id)}}" class="btn btn-primary float-end">Edit</a>


                  @csrf
                  @method('DELETE')
                 <button class="btn btn-danger float-end" type="submit">Delete</button>
                </form>
            </td>
        </tr>
        @endforeach
   
  </table>

i tried php artisan storage:link and creating the folder manually but it didnt work


Solution

  • i managed to fix this by changing my code like this, by explicitly defining the image object when creating the request :

     if ($request->hasFile('image')) {
    
            $file = $request->file('image');
            $extension = $file->getClientOriginalExtension();
            $filename = time() . '.' . $extension;
            $file->storeAs('images', $filename, 'public');
        }
    
        $data = $request->all();
        $data['image'] = $filename ?? 'no image';
        
       
        Category::create($data);
    
        return redirect()->route('category.index')->with('success', 'Category was added successfully !');