php - 如何绑定(bind)响应数据以在laravel 5中查看

标签 php mysql laravel laravel-5 httpresponse

我试图在元素上显示图像,但它不起作用,当我只是返回数据时,它显示图像。

路线.php

Route::get('/dashboard',function() {

       $Image = Auth::user()->profile_pic;
       $type = 'image/jpeg';
       $img = response($Image)->header('Content-Type', $type);

       return View::make('dashboard', ['img'=>$img]);
     });

仪表板.blade.php

<img src={{ $img }} class="img-circle" width="200" height="200">

请帮忙。

最佳答案

您应该添加一个路由,以便当请求发送到指定的 URL 时,从数据库中读取头像并将其发送到浏览器,如 image/jpeg。

例如将以下代码添加到您的routes.php

Route::any('/user/{user}/profile-pic',
    function(\App\User $user) {
       $Image = Auth::user()->profile_pic;
       return response($Image)->header('Content-Type', 'image/jpeg');
    });

修改/dashboard 路由以使用此新路由

Route::get('/dashboard',function() {
       $id = Auth::user()->id;
       $imageUrl="/user/$id/profile-pic";
       return view('/dashboard', ['imageUrl' => $imageUrl]);
     });

最后,在你看来,绑定(bind)新变量

<img src="{{$imageUrl}}" class="img-circle" width="200" height="200">

关于php - 如何绑定(bind)响应数据以在laravel 5中查看,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36820880/

相关文章:

PHP检查是否设置了数组索引并且没有超出范围

php - 显示调度算法

mysql - 如何编写一对一映射的 SQL 查询

php - 从 mysql 获取 - laravel

php - 处理不可避免的警告的正确方法

php - Yii2:使用 Swiftmailer 插件 (Openbuildings\Swiftmailer\CssInlinerPlugin)

php - 具有 PHP 或 Python 绑定(bind)的可编写脚本的 JavaScript 解释器?

php - 针对 mysql 注入(inject)的文本区域保护 - PHP

validation - Laravel 4验证唯一(数据库)忽略当前

php - Laravel 规则和正则表达式 (OR) 运算符的问题