2

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

3
  • whr is your image tag with url? Commented Feb 4, 2020 at 5:24
  • If you mean storing photos in a database, You need to save the image as a base64 in database.The image is stored in the database without saving to the server.The image path is stored in the code above. Commented Feb 4, 2020 at 6:03
  • @user11010754 could you please check my answer and give us a feed back Commented Feb 4, 2020 at 6:25

3 Answers 3

2

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

Sign up to request clarification or add additional context in comments.

11 Comments

wouldnt this still print the url rather than showing the image as img tag is not thr?
@Cerlin ok i added image tag as well
@KamleshPaul please check his code.Hes saving original file name into the table.
@NipunTharuksha he is only storing image name not storage_path()
@KamleshPaul yes he is renaming image but saving original name to db. So he cant find a image with that name in his uploads folder
|
1

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;

1 Comment

please check his code.Hes saving original file name into the table.
0
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.

1 Comment

` <img src="{{ Storage::disk('public')->url('/uploads/'.$blog->image) }}" > ` to view the image for the above code

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.