Home > OS >  How to display image in the view after get it from database laravel
How to display image in the view after get it from database laravel

Time:03-26

  • image path on database /storage/uploads/image.png

  • the actual path on my project storage/app/public/uploads/image.png

  • Controller:

public function insertCategory(Request $request)
    {
        $request->validate([
            "image"        => "required|image|mimes:jpg,png,jpeg,gif,svg|max:2048",
            "name"         => "required|min:2|max:20",
            "is_active"    => "required",
        ]);

        $category = new Category();

        if ($request->file('image')) {
            $imagePath = $request->file('image');
            $imageName =  str_replace(" ","-", $imagePath->getClientOriginalName());
            $path = $request->file('image')->storeAs('uploads', $imageName, 'public');
            $category->image = '/storage/' . $path;
        }

        $category->name = $request->name;
        $category->is_active = $request->is_active;
        $category->save();

        return redirect('add-category')->with('status','category added!');
    }
  • view.blade
<table >
  <thead>
       <th>Category Image</th>
       <th>Category Name</th>
       <th>Is Active</th>
  </thead>
  <tbody>
      @foreach($categories as $category)
          <tr>
             <td><img alt="image" src="{{Storage::url($category->image)}}"/</td>
             <td>{{$category->name}}</td>
             <td>{{$category->is_active}}</td>
          </tr>
      @endforeach
  </tbody>

try a bunch of solutions on stackoverflow, no one is right.

CodePudding user response:

Controller

    // Add a category
  public function insertCategory(Request $request)
    {
   //Data validation
      $validatedData = $this->validate($request, [
            "image"        => "required|image|mimes:jpg,png,jpeg,gif,svg|max:2048",
            "name"         => "required|min:2|max:20",
            "is_active"    => "required",
      ]);

      // Image saving
        $image = $request->file('image');
        $input['imagename'] = $request->file('image')->getClientOriginalName();
        $destinationPath = public_path('storage/uploads');
        $image->move($destinationPath, $input['imagename']);
 
     // Collect necessary data to write to the DB
     $validatedData = ([
            'title' => $request->name,
            'image' => $image,          
             $category->is_active = $request->is_active
      ]);

     // Creating related row in DB
      Categories::create($validatedData);

     // Return to the front with success message
      return redirect()->route('add-category')->withSuccess('category added!');
    }

View

<table >
  <thead>
       <th>Category Image</th>
       <th>Category Name</th>
       <th>Is Active</th>
  </thead>
  <tbody>
      @foreach($categories as $category)
          <tr>
             <td><img src="{{ asset('storage/uploads/'.$category->image) }}" alt="{{ $category->name }}" ></td>
             <td>{{$category->name}}</td>
             <td>{{$category->is_active}}</td>
          </tr>
      @endforeach
  </tbody>
  • Related