簡體   English   中英

刀片視圖中的未定義變量錯誤

[英]undefined variable error in blade view

我目前正在使用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();
}

在我的默認視圖中,我嘗試使用count(),但是每次加載頁面時,都會給我Undefined variable 我應該如何顯示?

嘗試更改此:

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

對此:

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

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

我認為您必須嘗試以下方法:

代替:

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

嘗試這個:

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

是的,從中刪除引號:

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

With函數帶有兩個參數key和value

你可以用這個

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

您可以嘗試以下方法:

代替:

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

嘗試這個:

return $this->returnemoji($path);

希望這對您有幫助。

有一些問題。

  1. 單引號不處理變量,所以代替此

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

    你可以這樣做

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

    要么

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

    但這並不是必需的,因為您僅使用變量本身而無需任何其他格式,因此請完全刪除引號

     return Image::get($file)->response(); 
  2. 對象運算符->在對象范圍內用於訪問對象的方法和屬性。 您的函數returnemoji()不是RedirectResponse類的方法,而這是redirect()幫助器方法所返回的。

  3. with()方法在這里不合適,您只需要將參數傳遞給這樣的函數

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

可選地,我建議遵循PSR2代碼樣式標准 ,其中包括駝峰式變量名,因此createemoji()應該是createEmoji() 我也認為在Laravel中返回大多數數據類型時,通常可以省略response() ,因為它將為您自動處理。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM