I have many input fields with Dropzone.js in one form, and I want submit that fields along with Dropzone.js. For technically explanation, I have UserAdminController.php with postUpload() function. In this postUpload() there are two return, the first return, pass to ImageRepository.php, and the last one redirect to create view after validate the input fields and save the data input. All I want to do is, make these two return work together, the problem lays here, they can't work together, if I run the first return, the second one doesn't work, and vice versa.
UserAdminController.php
class UserAdminController extends Controller {
public function postUpload(Request $request) {
//Save selected preview image and pass them to ImageRepository.php at the function upload()
$photo = Input::all();
$response = $this->image->upload($photo);
return $response;
//End save selected image
$messages = [
'avatar.required' => 'JPG and PNG is a must, and Minimal size of file must be 20KB and maximal 1MB'
];
$validation = \Validator::make($request->all(),[
"name" => "required|min:5|max:100",
"username" => "required|min:5|max:20|unique:admin",
"roles" => "required",
"phone" => "required|digits_between:10,12",
"address" => "required|min:20|max:200",
"avatar" => "required|image|mimes:jpg,jpeg,png,gif|max:1000",
"email" => "required|email|unique:admin",
"password" => "required",
"password_confirmation" => "required|same:password"
], $messages)->validate();
$new_user = new \App\Admin; //Panggil model Admin
$new_user->name = $request->get('name');
$new_user->username = $request->get('username');
$new_user->roles = json_encode($request->get('roles'));
$new_user->address = $request->get('address');
$new_user->phone = $request->get('phone');
$new_user->email = $request->get('email');
$new_user->password = \Hash::make($request->get('password'));
$avatar = $request->file('avatar');
if($avatar){
$file = $request->file('avatar')->store('avatars', 'public');
$new_user->avatar = $file;
}
$new_user->save();
return redirect()->route('usersadmin.create')->with('status', 'User successfully created');
}
}
ImageRepository.php
class ImageRepository {
public function upload( $form_data )
{
$validator = \Validator::make($form_data, Image::$rules, Image::$messages);
if ($validator->fails()) {
return Response::json([
'error' => true,
'message' => $validator->messages()->first(),
'code' => 400
], 400);
}
$photo = $form_data['file'];
$originalName = $photo->getClientOriginalName();
$extension = $photo->getClientOriginalExtension();
$originalNameWithoutExt = substr($originalName, 0, strlen($originalName) - strlen($extension) - 1);
$filename = $this->sanitize($originalNameWithoutExt);
$allowed_filename = $this->createUniqueFilename( $filename, $extension );
$uploadSuccess1 = $this->original( $photo, $allowed_filename );
$uploadSuccess2 = $this->icon( $photo, $allowed_filename );
if( !$uploadSuccess1 || !$uploadSuccess2 ) {
return Response::json([
'error' => true,
'message' => 'Server error while uploading',
'code' => 500
], 500);
}
$sessionImage = new Image;
$sessionImage->filename = $allowed_filename;
$sessionImage->original_name = $originalName;
$sessionImage->save();
return Response::json([
'error' => false,
'code' => 200,
'filename' => $allowed_filename
], 200);
}
}
via Chebli Mohamed
Aucun commentaire:
Enregistrer un commentaire