我的控制器是
public function store(Request $request) { if (User::where('phone_number', '=', Input::get('phone_number'))->exists()) { return $this->respondBadRequest('Phone Number Exists'); } else { User::create($request->all()); return redirect('agents')->with('Success', 'Agent Added'); if($request->hasFile('image')) { $file = $request->file('image'); //you also need to keep file extension as well $name = $file->getClientOriginalName().'.'.$file->getClientOriginalExtension(); //using array instead of object $image['filePath'] = $name; $file->move(public_path().'/uploads/', $name); } } }
我想我缺少了ajax posting
,但是我无法弄清楚
一世 dd($request->all());
结果是
array:9 [? "_token" => "heSkwHd8uSIotbqV1TxtAoG95frcRTATgeGL0aPM" "name" => "fwe" "email" => "sanjiarya2112@gmail.com" "phone_number" => "4444422555" "address_city_village" => "sgf" "address_state" => "gfdgsdf" "password" => "ffffff" "confirm" => "ffffff" "image" => UploadedFile {#208 ? -test: false -originalName: "Screenshot (8).png" -mimeType: "image/png" -size: 135920 -error: 0 path: "C:\wamp\tmp" filename: "php47F2.tmp" basename: "php47F2.tmp" pathname: "C:\wamp\tmp\php47F2.tmp" extension: "tmp" realPath: "C:\wamp\tmp\php47F2.tmp" aTime: 2017-01-24 06:14:40 mTime: 2017-01-24 06:14:40 cTime: 2017-01-24 06:14:40 inode: 0 size: 135920 perms: 0100666 owner: 0 group: 0 type: "file" writable: true readable: true executable: false file: true dir: false link: false linkTarget: "C:\wamp\tmp\php47F2.tmp" } ]
我在C:\wamp\tmp\php47F2.tmp
那儿检查了enen我找不到图像
期待急需的帮助
谢谢
上载文件时尝试使用FormData
in ajax
。
试试这个
$('form').submit(function(event) { event.preventDefault(); var formData = new FormData($(this)[0]); $.ajax({ url: '{{ url('/agents') }}', type: 'POST', data: formData, success: function(result) { location.reload(); }, error: function(data) { console.log(data); } }); });
要么
你可以尝试一下 jQuery
library
https://github.com/malsup/form
编辑
public function store(Request $request) { if (User::where('phone_number', '=', Input::get('phone_number'))->exists()) { return $this->respondBadRequest('Phone Number Exists'); } else { $user=User::create($request->all()); if($request->hasFile('image')) { $file = $request->file('image'); //you also need to keep file extension as well $name = $file->getClientOriginalName().'.'.$file->getClientOriginalExtension(); //using the array instead of object $image['filePath'] = $name; $file->move(public_path().'/uploads/', $name); $user->image= public_path().'/uploads/'. $name; $user->save(); } return redirect('agents')->with('Success', 'Agent Added'); } }