【问题标题】:Laravel retrieve images from databaseLaravel 从数据库中检索图像
【发布时间】:2017-11-03 15:17:59
【问题描述】:

我需要在我的视图中显示存储的图像。我使用下面的代码将图像存储在数据库中,

     $file = Input::file('pic');
     $img = Image::make($file);
     Response::make($img->encode('jpeg'));
     $picture = new Car;
    $picture->cartype = Input::get('cartype');
    $picture->price = Input::get('price');

    $FileName            = Input::file('pic')->getClientOriginalName();
    $FilePath = public_path('') .'/images/'. $FileName;
    $picture->name = $FilePath;
    $picture->save();

在我的控制器中(检索图像)

public function index()
{

            $cars = DB::table('car_category')->get();
           return view('category.category',['cars'=>$cars]);


} 

我应该如何检索和显示视图中的图像?

【问题讨论】:

    标签: php laravel-5


    【解决方案1】:

    如果您将图像名称存储在数据库中并将其上传到目录中,那么您只需从数据库中获取名称,返回名称以查看并将名称附加到硬编码的目录地址,即可轻松显示它这-

    <img src="/images/{{ $imageVariableName }}">
    

    让我知道这是否有效。

    【讨论】:

    • 谢谢。我已经试过了。@foreach( $cars as $car ) &lt;tr class="active"&gt; &lt;td&gt;{{$car-&gt;id}}&lt;/td&gt; &lt;td&gt;{{$car-&gt;cartype}}&lt;/td&gt; &lt;td&gt;{{$car-&gt;price}}&lt;/td&gt; &lt;td&gt;{{$car-&gt;seats}}&lt;/td&gt; &lt;td&gt;&lt;img src="{{ $car-&gt;name }}"&gt;&lt;/td&gt;
    • 返回空行
    • 我的存储路径有问题吗?我在数据库中存储为 C:\xampp\htdocs\laravel\movecab\public/images/1509446707.jpg跨度>
    • 只需将1509446707.jpg 存储在数据库中并使用 - name }} 在视图中检索它
    • 我已经尝试过了,我在控制台中收到错误消息“加载资源失败:服务器响应状态为 404(未找到)”
    猜你喜欢
    • 2021-07-06
    • 2017-09-02
    • 2019-11-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-10-07
    • 1970-01-01
    相关资源
    最近更新 更多