Laravel显示数据库中的图像


Laravel Displaying image from database

所以我想在视图中显示图像而不是图像名称。

我的观点是

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

我想要的是显示实际的图像,而不是图像的名称。我怎样才能做到这一点?我应该先将图像保存在公用文件夹中吗?或者我的方法错了?请开导我一下?非常感谢。我试过这种方法

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

但我仍然得到相同的输出。

首先,您必须将图像存储在公共目录中(如果不是,则浏览器无法访问它们)

然后它取决于$post->image实际上是什么。如果它是相对于public的路径,你可以这样做:

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

或者:

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

编辑

由于您的图像存储在public/img中,并且image属性仅包含名称(不包含img/),因此您必须准备:

<img src="{{ asset('img/' . $post->image) }}" />
  1. 插入您的图像:

    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. 按索引显示

     public function index()
        {
            $pages = Appsetting::all();
            return view('admin.appsettings.pages', compact('pages',$pages));
        }
    
  3. 来到刀锋档案。(pages.blade.php

    @foreach ($pages as $page)
    <td> <img src="{{ asset('uploads/appsetting/' . $page->image) }}" /> </td>
    @endforeach
    
  4. 设定你的路线并100%工作。

您应该在视图中这样写代码

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

您可以像这样使用<img src="{{ asset('img')}}/{{ $post->image) }}" />

您的图像目录是public/img/image的名称

图像已保存到数据库中,但您不知道图像保存到磁盘上的哪个文件夹,所以请查看控制器以查看图像保存的位置。

然后在刀片中您可以使用:

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

如果您的图像在public/images文件夹中,请使用以下内容。否则,请使用公共文件夹中该图像的项目路径。连接图像扩展名。

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

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

在这种情况下如何获取图像?

在这里,我找到了上述问题的解决方案。在映像..的迁移表中。。在这之后,制作一个播种机并为播种机添加以下内容,你可以在youtube上找到教程。现在,获取部分在这里打开您的blade文件并添加行,重要的是数据获取将仅在循环中。

  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. 我很快就会把我的电子商务项目上传到Github书签下面的链接来继续关注。https://github.com/Anshu1802