如何绑定响应数据以在 laravel 5 中查看
How to bind response data to view in laravel 5
我试图在元素上显示图像,但它不起作用,当我只是返回显示图像的数据时。
route.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]);
});
dashboard.blade.php
<img src={{ $img }} class="img-circle" width="200" height="200">
请帮忙。
Auth::user()->profile_pic
是用户头像的URL吗?
如果是这样,只需使用
return View::make('dashboard', ['img'=>Auth::user()->profile_pic]);
此外,为 src 属性添加引号:
<img src="{{ $img }}" class="img-circle" width="200" height="200">
重要的是要知道
$img = response($Image)->header('Content-Type', $type);
是一个响应对象,它是Illuminate\Http\Response
,但是在HTML中你需要一个link到图像,一个string
,而不是一个[=16] =]
你应该添加一个路由,这样当请求发送到指定的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]);
});
最后,在您看来,绑定新变量
<img src="{{$imageUrl}}" class="img-circle" width="200" height="200">
我试图在元素上显示图像,但它不起作用,当我只是返回显示图像的数据时。
route.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]);
});
dashboard.blade.php
<img src={{ $img }} class="img-circle" width="200" height="200">
请帮忙。
Auth::user()->profile_pic
是用户头像的URL吗?
如果是这样,只需使用
return View::make('dashboard', ['img'=>Auth::user()->profile_pic]);
此外,为 src 属性添加引号:
<img src="{{ $img }}" class="img-circle" width="200" height="200">
重要的是要知道
$img = response($Image)->header('Content-Type', $type);
是一个响应对象,它是Illuminate\Http\Response
,但是在HTML中你需要一个link到图像,一个string
,而不是一个[=16] =]
你应该添加一个路由,这样当请求发送到指定的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]);
});
最后,在您看来,绑定新变量
<img src="{{$imageUrl}}" class="img-circle" width="200" height="200">