2016-08-14 246 views
0

我正尝试使用表单上传图片。我总共有5个领域,不想让所有领域都需要。但是,如果我将其中一个字段留空,则会有例外。但是当我上传所有5张图像时,一切正常。Laravel 5图片上传

我在我的$ rules数组中没有规则。 isValid()出现问题。
错误:

FatalErrorException in ProfilesController.php line 191: 
Call to a member function getClientOriginalExtension() on a non-object. 

有人能指出我到正确的方向吧。

我的控制器:

public function update(ProfileRequest $request, $id) 
    { 
     $profile = Profile::findOrFail($id); 

     $request->merge([ 'wifi' => $request->has('wifi') ? true : false, 
          'takeaway'=> $request->has('takeaway') ? true : false, 
          'ec'=> $request->has('ec') ? true : false, 
          'creditcard'=> $request->has('creditcard') ? true : false, 
          'cash'=> $request->has('cash') ? true : false, 
          'wheelchair'=> $request->has('wheelchair') ? true : false, 
          'outdoor'=> $request->has('outdoor') ? true : false, 
          'tv'=> $request->has('tv') ? true : false, 
          'service'=> $request->has('service') ? true : false, 
          'smoking'=> $request->has('smoking') ? true : false, 
          'reservation'=> $request->has('reservation') ? true : false, 
          'brunch'=> $request->has('brunch') ? true : false, 
         ]); 





     // getting all of the post data 
     $file = array('image_profilehero' => Input::file('image_profilehero'), 
        'image_avatar' => Input::file('image_avatar'), 
        'pdf' => Input::file('pdf'), 
        'restaurantscene1' => Input::file('restaurantscene1'), 
        'restaurantscene2' => Input::file('restaurantscene2') 
      ); 

     // setting up rules 
     $rules = array(//'image_profilehero' => 'required', 
        //'image_avatar' => 'required' 

      ); //mimes:jpeg,bmp,png and for max size max:10000 

     // doing the validation, passing post data, rules and the messages 
     $validator = Validator::make($file, $rules); 
     if ($validator->fails()) { 
     // send back to the page with the input data and errors 
     return Redirect::to('backend/profile')->withInput()->withErrors($validator); 
     } 
     else { 
     // checking file is valid. 
     if (Input::file('image_profilehero') or Input::file('image_avatar')->isValid() or Input::file('pdf')->isValid() or Input::file('restaurantscene1')->isValid() or Input::file('restaurantscene2')->isValid()) { 
      $destinationPathAvatar = 'uploads/avatar'; // upload path Avatar 
      $destinationPathProfileHero = 'uploads/profilehero'; // upload path ProfileHero 
      $destinationPathPdf = 'uploads/speisekarten'; // upload path ProfileHero 

      //Restaurant Scene Bilder 
      $destinationPathRestaurantScene1 = 'uploads/restaurantscene'; // upload path RestaurantScene 
      $destinationPathRestaurantScene2 = 'uploads/restaurantscene'; // upload path RestaurantScene 

      $extensionAvatar = Input::file('image_avatar')->getClientOriginalExtension(); // getting image extension 
      $extensionProfileHero = Input::file('image_profilehero')->getClientOriginalExtension(); // getting image extension 
      $extensionPdf = Input::file('pdf')->getClientOriginalExtension(); // getting image extension 

      $extensionRestaurantScene1 = Input::file('restaurantscene1')->getClientOriginalExtension(); // getting image extension 
      $extensionRestaurantScene2 = Input::file('restaurantscene2')->getClientOriginalExtension(); // getting image extension 

      //$fileName = rand(11111,99999).'.'.$extension; // renameing image 

      $fileNameAvatar = '/avatar/avatar_'.Auth::user()->id.'.'.$extensionAvatar; 
      $fileNameProfieHero = '/profilehero/profilehero_'.Auth::user()->id.'.'.$extensionProfileHero; 
      $fileNamePdf = '/speisekarten/pdf_'.Auth::user()->id.'.'.$extensionPdf; 

      $fileNameRestaurantScene1 = '/restaurantscene/scene1_'.Auth::user()->id.'.'.$extensionRestaurantScene1; 
      $fileNameRestaurantScene2 = '/restaurantscene/scene2_'.Auth::user()->id.'.'.$extensionRestaurantScene2; 

      Input::file('image_profilehero')->move($destinationPathProfileHero, $fileNameProfieHero); // uploading file to given path 
      Input::file('image_avatar')->move($destinationPathAvatar, $fileNameAvatar); // uploading file to given path 
      Input::file('pdf')->move($destinationPathPdf, $fileNamePdf); // uploading file to given path 

      Input::file('restaurantscene1')->move($destinationPathRestaurantScene1, $fileNameRestaurantScene1); // uploading file to given path 
      Input::file('restaurantscene2')->move($destinationPathRestaurantScene2, $fileNameRestaurantScene2); // uploading file to given path 

      // sending back with message 
      return redirect('backend/profile')->with([ 
      'flash_message_important' => false, 
      'flash_message' => 'All done' 
      ]); 
     } 
     else { 
      // sending back with error message. 
      return redirect('backend/profile')->with([ 
      'flash_message_important' => false, 
      'flash_message' => 'Upps. Something's wrong.' 
      ]); 
     } 
     } 
     //return redirect('backend/profile'); 
     $profile->update($request->all()); 
    } 

回答

1

你的代码不占空或丢失的文件,它只是假设有有一个文件,并试图move它。所以你最终调用非对象的方法,可能是null。你只需做一些额外的工作,以确保你确实有对象打电话之前它们的方法,像这样:

$pdf = Input::file('pdf'); 
if ($pdf) { 
    $extension = $pdf->getClientOriginalExtension(); 
    $pdf->move($destinationPathPdf, $fileNamePdf); 
} 

这样一来,如果没有PDF文件,if语句将是错误的,并会跳过它的调用方法,所以你避免了这种错误。这样做通常是很好的做法。