app/Http/Controllers/admin/UsersController.php
/**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
Store a newly created resource in storage.
Store a newly created resource in storage .
public function store(Request $request)
    {   
         $this->validate($request, [
            'username' => 'required|unique:jc_users',
        ],[
            'username.unique' => '用户名已存在',
        ]);
        $data = $request -> all();
        $data['password'] = Hash::make($data['password']);
        if($request -> hasFile('face')){
            $profile = $request -> file('face');
            $ext = $profile->getClientOriginalExtension();
            $temp_name = str_random(20);
            $filename =  $temp_name.'.'.$ext;
            $dirname = date('Ymd',time());
            $profile -> move('./uploads/hpic/'.$dirname,$filename); 
            $fileadd = ('/uploads/hpic/'.$dirname.'/'.$filename); 
            $uid = DB::table('jc_users')->insertGetId(['username'=>$data['username'],'email'=>$data['email'],'password'=>$data['password'],'grade'=>$data['grade']]);
            $res = DB::table('jc_user_details')->insert(['uid'=>$uid,'face'=>$fileadd,'nickname'=>$data['nickname'],'id_card'=>$data['id_card'],'phone'=>$data['phone'],'sex'=>$data['sex'],'addr'=>$data['addr']]);
            if ($res == true) {
                return redirect('/admin/users/index')-> with('success','添加成功');
            }else{
                return back()->with('error','添加失败');
            }
        } else {
            $fileadd = ('/uploads/hpic/mr/'.'CHZEvbLFV707vZ1ONYuT.jpg'); 
            $uid = DB::table('jc_users')->insertGetId(['username'=>$data['username'],'email'=>$data['email'],'password'=>$data['password'],'grade'=>$data['grade']]);
            $res = DB::table('jc_user_details')->insert(['uid'=>$uid,'face'=>$fileadd,'nickname'=>$data['nickname'],'id_card'=>$data['id_card'],'phone'=>$data['phone'],'sex'=>$data['sex'],'addr'=>$data['addr']]);
            if ($res == true) {
                return redirect('/admin/users/index')-> with('success','添加成功');
            }else{
                return back()->with('error','添加失败');
            }
        } 
    }
public function store ( Request $ request ) { $ this -> validate ( $ request , [ 'username' => 'required|unique:jc_users' , ] , [ 'username.unique' => '用户名已存在', ] ) ; $ data = $ request -> all ( ) ; $ data [ 'password' ] = Hash :: make ( $ data [ 'password' ] ) ; if ( $ request -> hasFile ( 'face' ) ) { $ profile = $ request -> file ( 'face' ) ; $ ext = $ profile -> getClientOriginalExtension ( ) ; $ temp_name = str_random ( 20 ) ; $ filename = $ temp_name . '.' . $ ext ; $ dirname = date ( 'Ymd' , time ( ) ) ; $ profile -> move ( './uploads/hpic/' . $ dirname , $ filename ) ; $ fileadd = ( '/uploads/hpic/' . $ dirname . '/' . $ filename ) ; $ uid = DB :: table ( 'jc_users' ) -> insertGetId ( [ 'username' => $ data [ 'username' ] , 'email' => $ data [ 'email' ] , 'password' => $ data [ 'password' ] , 'grade' => $ data [ 'grade' ] ] ) ; $ res = DB :: table ( 'jc_user_details' ) -> insert ( [ 'uid' => $ uid , 'face' => $ fileadd , 'nickname' => $ data [ 'nickname' ] , 'id_card' => $ data [ 'id_card' ] , 'phone' => $ data [ 'phone' ] , 'sex' => $ data [ 'sex' ] , 'addr' => $ data [ 'addr' ] ] ) ; if ( $ res == true ) { return redirect ( '/admin/users/index' ) -> with ( 'success' , '添加成功'); } else { return back ( ) -> with ( 'error' , '添加失败'); } } else { $ fileadd = ( '/uploads/hpic/mr/' . 'CHZEvbLFV707vZ1ONYuT.jpg' ) ; $ uid = DB :: table ( 'jc_users' ) -> insertGetId ( [ 'username' => $ data [ 'username' ] , 'email' => $ data [ 'email' ] , 'password' => $ data [ 'password' ] , 'grade' => $ data [ 'grade' ] ] ) ; $ res = DB :: table ( 'jc_user_details' ) -> insert ( [ 'uid' => $ uid , 'face' => $ fileadd , 'nickname' => $ data [ 'nickname' ] , 'id_card' => $ data [ 'id_card' ] , 'phone' => $ data [ 'phone' ] , 'sex' => $ data [ 'sex' ] , 'addr' => $ data [ 'addr' ] ] ) ; if ( $ res == true ) { return redirect ( '/admin/users/index' ) -> with ( 'success' , '添加成功'); } else { return back ( ) -> with ( 'error' , '添加失败'); } } }
Store a newly created resource in storage.
Store a newly created resource in storage .
//查询用户表中的所有用户 //获取数据 // dd($data); //处理保存图片 // 使用request 创建文件上传对象 // 获取文件后缀名 // 处理文件名称 // 保存文件 //获取最后插入数据的ID号 //添加用户详情表中的数据 //对数据的添加进行整体的判断 //设置用户的默认头像 //获取最后插入数据的ID号 //添加用户详情表中的数据 //对数据的添加进行整体的判断