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());
}