2017-02-09 34 views
0

等我有這樣的事情:圖像驗證工作在一個形式,但不會在Laravel

myitemrequest.php:

public function authorize() 
    { 
     return true; 
    } 

public function rules() 
    { 
     return [ 
      'avatar_pic' => 'image', 
     ]; 
    } 

這驗證這一點:

if ($validator->fails()) { 
       return back() 
       ->withErrors($validator) 
       ->withInput(); 
     } 
if(request()->file("avatar_upload")) { 
     $file = request()->file("avatar_upload"); 
    $file->storeAs('public/avatars/' . Auth::user()->id , "avatar.jpg"); 
} 
return back(); 

這完美的作品。現在,由於某種原因,這一點:

otherequest.php:

public function authorize() 
{ 
    return true; 
} 

public function rules() 
{ 
    return [ 
     'animage' => 'image', 
     'anotherimage' => 'image', 
     'anotherimage2' => 'image', 
     'anotherimage3' => 'image' 
    ]; 
} 

這證實了這一點:

if ($backgroundImagesRequest->authorize() == false) { 
     return back() 
      ->withErrors($worklow_request) 
      ->withInput(); 
    } 

    if(isset($backgroundImagesRequest->animage)) 
    { 

     $file = request()->file("animage"); 
     $file->storeAs('public/background_images/' . Auth::user()->id , "animage.jpg"); 
    } 

    if(isset($backgroundImagesRequest->anotherimage1)) 
    { 

     $file = request()->file("anotherimage1"); 
     $file->storeAs('public/background_images/' . Auth::user()->id , "anotherimage1.jpg"); 
    } 

    if(isset($backgroundImagesRequest->anotherimage2)) 
    { 

     $file = request()->file("anotherimage2"); 
     $file->storeAs('public/background_images/' . Auth::user()->id , "anotherimage2.jpg"); 
    } 

    if(isset($backgroundImagesRequest->anotherimage3)) 
    { 

     $file = request()->file("anotherimage3"); 
     $file->storeAs('public/background_images/' . Auth::user()->id , "anotherimage3.jpg"); 
    } 

    return back(); 
} 

都不行,它只是給我發回來的錯誤消息「文件必須成爲一個形象!「。我明顯嘗試了與頭像上傳完全相同的圖像,並且相應地進行了驗證和存儲。它根本不會發生背景圖像。

任何想法可能出現錯誤?

回答

2

在你的第一個「工作」比如你有一個驗證

if ($validator->fails()) { 
    return back() 
    ->withErrors($validator) 
    ->withInput(); 
} 

而在第二你有

if ($backgroundImagesRequest->authorize() == false) { 
    return back() 
    ->withErrors($worklow_request) 
    ->withInput(); 
} 

,因爲您的驗證是不是在這兩個例子中相同的替換代碼

+0

其實兩者都正確工作。我忘了將enctype =「multipart/form-data」添加到

。 – prgrm