2017-02-09 8 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

関連する問題