2016-09-29 104 views
1

我目前正在使用Laravel 5.2,嘗試在點擊 上顯示圖像,這些圖像當前存儲在存儲文件夾中。我試圖在我的刀片視圖中顯示這些圖像,但每次加載頁面時,都會遇到未定義的變量異常。刀片視圖中未定義的變量錯誤

控制器

public function createemoji($action,$statusId) 
{ 
    $path = storage_path('app/public/images/'.$action.'.gif'); 

    /*$request=new storage(); 
    $request->comment=$path; 
    $request->user_id=Auth::user()->id; 
    $request->post_id=$statusId; 
    $request->save();*/ 
    return redirect()->returnemoji()->with('file'->$path); 

} 

public function returnemoji($file) 
{   
    return Image::get('$file')->response(); 
} 

在我的默認視圖我試着用計數(),但每次它加載的頁面,它給了我未定義的變量。我應該如何顯示它?

回答

2

試圖改變這一點:

->with('file'->$path); 

要這樣:

->with('file', $path); 

https://laravel.com/docs/5.3/views#passing-data-to-views

+0

它仍未顯示在刀片​​視圖中,它表示未定義的變量。 –

+0

您還需要刪除此行中文件變量的引號:return Image :: get($ file) - > response(); –

+0

仍然無效,未定義變量.. –

0

相反的:

return redirect()->returnemoji()->with('file'->$path); 

試試這個:

從這個
return redirect()->returnemoji($path); 

是的,去掉引號:

return Image::get('$file')->response(); 
0

隨着函數有兩個參數鍵和值

您可以使用此

return redirect()->returnemoji()->with('file',$path); 
0

你可以試試這個:

而不是:

return redirect()->returnemoji()->with('file'->$path); 

試試這個:

return $this->returnemoji($path); 

希望這有助於你。

0

有幾個問題。

  1. 單引號不處理變量,所以不是這個

    return Image::get('$file')->response(); 
    

    你能做到這一點

    return Image::get("$file")->response(); 
    

    return Image::get("{$file}")->response(); 
    

    但這些都不是necssary因爲你只是使用了通過本身變量而沒有任何額外的格式,所以除去共

    return Image::get($file)->response(); 
    
  2. 目的操作者->在對象範圍內用於訪問的方法和對象的屬性引號。您的功能returnemoji()不是RedirectResponse類的方法,這是redirect()輔助方法返回的內容。

  3. with()方法不適合這裏,你只需要一個參數傳遞給一個函數這樣

    return redirect()->returnemoji($path); 
    

可選,我建議按照PSR2 code style standard其中包括駱駝套管變量名稱,以便createemoji()應該是createEmoji()。另外,我認爲在返回Laravel中的大多數數據類型時通常可以省略response(),因爲它會自動爲您處理。