'Laravel Displaying image from database

so i wanted to display the image not the image name in my views.

i got this in my views

<img src="{{$post->image}}" />

what i want is to display the actual image not the name of the image. how can i achieve this? should i save the image first in the public folder? or my method is wrong? Enlighten me please? thank you so much. i have tried this method

{{ HTML::image($post->image, '', array('class' => 'image')); }}

but i still get the same output.



Solution 1:[1]

First, you have to store the images in the public directory (if not the browser can't access them)

Then it depends what $post->image actually is. If it is a path relative to public you can do this:

<img src="{{ asset($post->image) }}" />

Or:

{{ HTML::image($post->image, '', array('class' => 'image')); }}

Edit

Since you're images are stored in public/img and the image attribute only holds the name (without img/) you have to prepend that:

<img src="{{ asset('img/' . $post->image) }}" />

Solution 2:[2]

  1. Insert Your Image:

    public function store(Request $request)
     {
     $pages = new Appsetting();
    
            $pages->title = $request->input('title');
            $pages->description = $request->input('description');
    
            if ($request->hasfile('image')) {
                $file = $request->file('image');
                $extension = $file->getClientOriginalExtension(); // getting image extension
                $filename = time() . '.' . $extension;
                $file->move('uploads/appsetting/', $filename);
    
    //see above line.. path is set.(uploads/appsetting/..)->which goes to public->then create
    //a folder->upload and appsetting, and it wil store the images in your file.
    
                $pages->image = $filename;
            } else {
                return $request;
                $pages->image = '';
            }
           $pages->save();
    
           return redirect('pagesurl')->with('success', 'App Setting Added');
     }
    
  2. Display it by index

     public function index()
        {
            $pages = Appsetting::all();
            return view('admin.appsettings.pages', compact('pages',$pages));
        }
    
  3. Come to Blade file. (pages.blade.php)

    @foreach ($pages as $page)
    <td> <img src="{{ asset('uploads/appsetting/' . $page->image) }}" /> </td>
    @endforeach
    
  4. set your route and 100% working.

Solution 3:[3]

You can use like this <img src="{{ asset('img')}}/{{ $post->image) }}" />

Your image directory is public/img/image's name

Solution 4:[4]

The image was saved to the database, but you don't know which folder on disk the image was saved to, so look on your controller to see where it is saved.

Then in blade you can use:

<img src="/folder_name_path/{{$post ->image}}" />

Solution 5:[5]

if your image is in public/images folder use below.otherwise use the path in project for that image in the public folder.concatenate with the image extension.

<img src="images/{{$post->image}}.jpg" alt={{$post->image}}/>

Solution 6:[6]

style="url(background-image: images/bg.jpg);

How to fetch image in this case ?

Solution 7:[7]

Here, I found a solution to the above-mentioned question. In the migration table for image.. after this make a seeder and add the following for seeder you may find the tutorial on youtube. now the fetching part is here open your blade file and add the line to it and important thing is that data fetching will be in the loop only.

  1. $table->string('nameofthetable');
public function run()
{
    DB::table('products')->insert([
        [   
             'name'=>'Burger',
             'price'=>'69',
             'gallery'=>"search on google photo you  want and open an 
                        new tab and add a link over here to get the thing right ",
             'category'=>'western',
             'description'=>'A good and delicious burger at just cheaper price',
        ]
    ])
}
@foreach ( $products as $item )                    
    <div class="lg:w-1/4 md:w-1/2 p-4 w-full">
      <a class="block relative h-48 rounded overflow-hidden">
        <img alt="ecommerce" class="object-cover object-center w-full h-full block" src="{{ $item['gallery'] }}">
      </a>
      <div class="mt-4">
        <a class="active" href="{{ url('/detail{}') }}"><h3 class="text-gray-500 text-xs tracking-widest title-font mb-1">{{ $item['name'] }}</h3></a>
        <h2 class="text-gray-900 title-font text-lg font-medium">{{ $item['categeory'] }}</h2>
        <p class="mt-1">{{ $item['price'] }}</p>
      </div>
    </div>
@endforeach ()
  1. i will soon upload my E-commerce project on Github bookmark the below link to stay tuned . https://github.com/Anshu1802

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2 Dmitriy
Solution 3
Solution 4 ivcubr
Solution 5
Solution 6 Atika
Solution 7 Onur Demir