我试图在元素上显示图像,但它不起作用,当我只是返回数据时,它显示图像。
路线.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/