3

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.

8 Answers 8

5

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) }}" />
Sign up to request clarification or add additional context in comments.

4 Comments

The asset function will generate an absolute URI that points to your public directory. Normally something like: http://example.com/path/you/passed is the result.
ok i understand now but still its not working i dont know whats am i doing wrong.
Where is your image stored and what does $post->image return?
in public/img/ and $post->image returns the name of the photo.
1
  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.

Comments

1

you should write like this code in your view

<img src="{{asset('your path to folder of images in public folder/ '.$post->image)}}"/>

1 Comment

Good to see your first answer
0

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

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

Comments

0

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}}" />

Comments

0

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}}/>

Comments

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

How to fetch image in this case ?

2 Comments

<div class="item" style="background-image: url(images/banner_images/banner01.jpg);">
you should refrain from asking questions in answers, issue a new question linking to this question
0

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

Comments

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.