2016-04-24 78 views
0

我正在嘗試在元素上顯示圖像,但它不起作用,而當我僅僅將數據返回到其顯示圖像時。如何綁定響應數據以在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"> 

請幫忙。

回答

0

您應該添加一個路由,以便在將請求發送到指定的URL時,從數據庫讀取頭像並將其發送到瀏覽器,如image/jpeg。

E.g.下面的代碼添加到您的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'); 
    }); 

修改/儀表板途徑利用這條新航線的

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"> 
0

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,一個string,而不是一個Response

+0

我累了,但仍然無法正常工作 –

+0

任何錯誤信息?右鍵單擊並查看頁面源代碼,得到了什麼HTML? – Kevin

+0

沒有,但仍然沒有顯示圖像 –