CodeNew
CodeNew

Reputation: 49

Display the image from mysql database in php laravel

I'm trying to display image from mysql database. it's just showing the name of the image. Image is not displaying. how to make it display? I'm using laravel framework with localhost. Kindly look into it and help me out..

my controller page code...

public function stores(Request $request) {
        $this->validate($request, [
            'firstname' => 'required',
            'lastname' => 'required',
            'email' => 'required',
            //'image' => 'image|mimes:jpeg,png,jpg,svg|max:2048',
            'description' => 'required'
        ]);



        if($request->hasFile('image')){
          $image = $request->file('image');
          $filename = time() . '.' . $image->getClientOriginalExtension();
          Image::make($image)->resize(300, 300)->save( storage_path('/uploads/' . $filename ) );
          $blog->image = $filename;
        }

        $blog = new Blogform;
        $blog->first_name = $request->firstname;
        $blog->last_name = $request->lastname;
        $blog->email = $request->email;
        $blog->image = $request->image;
        $blog->description = $request->description;
        $blog->save();
        return redirect('/blog')->with('successMsg', 'Blog form Submitted Successfully');
    }



`my view page code..`

@foreach($blogs as $blog)
<tr>
    <th scope="row"> {{ $blog->id}} </th> 
    <td> {{ $blog->first_name}} </td> 
    <td> {{ $blog->last_name}} </td> 
    <td> {{ $blog->email}} </td> 
    <td> {{ $blog->image}} </td> 
    <td> {{ $blog->description}} </td> 
    <td> {{ $blog->created_at}} </td> 
    <td> {{ $blog->updated_at}} </td> 
</tr>
@endforeach

Upvotes: 2

Views: 985

Answers (3)

CodeNew
CodeNew

Reputation: 49

public function stores(Request $request) {
        $this->validate($request, [
            'firstname' => 'required',
            'lastname' => 'required',
            'email' => 'required|email',
            'image' => 'required|mimes:jpeg, png, jpg',
            'description' => 'required'
        ]);
         $image = $request->file('image');
        if(isset($image)) {

            //make unique name for the image
            $currentDate = Carbon::now()->toDateString();
            $imageName = $currentDate. '-' .$image->getClientOriginalExtension();

            //check for the presence of the directory
            if(!Storage::disk('public')->exists('uploads')) {
                Storage::disk('public')->makeDirectory('uploads');
            }
                //resize the image for uploads
                $uploads = Image::make($image)->resize(200, 200)->stream();
                Storage::disk('public')->put('uploads/' .$imageName, $uploads);

            } else {
            $blog->image = 'default.png'; 
        }
            $blog = new Blogform();   
            $blog->first_name = $request->firstname;
            $blog->last_name = $request->lastname;
            $blog->email = $request->email;
            $blog->image = $imageName;
            $blog->description = $request->description;
            $blog->save();
            return redirect('/blog')->with('successMsg', 'Blog form Submitted Successfully');

    }
```
The above codes works fine as it adds data perfectly to the database.

Upvotes: 0

Kamlesh Paul
Kamlesh Paul

Reputation: 12391

Modify you'r controller

$blog = new Blogform;
$blog->first_name = $request->firstname;
$blog->last_name = $request->lastname;
$blog->email = $request->email;

if ($request->hasFile('image')) {
    $image = $request->file('image');
    $filename = time() . '.' . $image->getClientOriginalExtension();
    Image::make($image)->resize(300, 300)->save(storage_path('/uploads/' . $filename));
    $blog->image = $filename;
     } else {
          $blog->image = '';
     }
     $blog->description = $request->description;
     $blog->save();
     return redirect('/blog')->with('successMsg', 'Blog form Submitted Successfully');
     }

use Storage::url() function with img tag

@foreach($blogs as $blog)
<tr>
    <th scope="row"> {{ $blog->id}} </th> 
    <td> {{ $blog->first_name}} </td> 
    <td> {{ $blog->last_name}} </td> 
    <td> {{ $blog->email}} </td> 
    <td> 
        <img src="{{  Storage::disk('public')->url('uploads/'.$blog->image) }}" alt="image" width="50">
    </td> 
    <td> {{ $blog->description}} </td> 
    <td> {{ $blog->created_at}} </td> 
    <td> {{ $blog->updated_at}} </td> 
</tr>
@endforeach

ref link https://laravel.com/docs/6.x/filesystem#file-urls

Note : - make sure in .env APP_URL is correct

Upvotes: 2

Dilip Hirapara
Dilip Hirapara

Reputation: 15296

use img src tag while show image

<td> 
  <img src="{{ Storage::url('uploads/'.$blog->image) }}" alt="image" width="50"> 
</td> 

As you modify the name and store different name so change the code as below while saving the blog

$blog->image = $filename;

Upvotes: 1

Related Questions