/** * Store a newly created resource in storage. * * @param \Illuminate\Http\Request $request * @return \Illuminate\Http\Response */ public function store(GarageRequest $request) { $data = $request->all(); $admin_array = array('username' => $data['username'], 'password' => bcrypt($data['password']), 'email' => $data['email'], 'realname' => $data['realname'], 'phone' => $data['phone'], 'user_type' => 'manager'); try { DB::transaction(function () use($admin_array, $data) { // 新建车厂管理员 $admin_id = DB::table('admins')->insertGetId($admin_array); $admin = User::where('id', $admin_id)->first(); // $admin = DB::table('admins')->where('id', $admin_id)->first(); $role = Role::where('name', 'Garage')->first(); // $role = DB::table('roles')->where('name', 'Garage')->first(); $admin->attachRole($role); // 设置车厂管理员权限 $garage_array = array('admin_id' => $admin_id, 'name' => $data['name'], 'branch_name' => $data['branch_name'], 'type' => $data['type'], 'location_detail' => $data['location_detail']); DB::table('garages')->insertGetId($garage_array); return redirect()->route('admin.checkgarage.index')->with('message', '成功添加新车厂!'); }); } catch (Exception $e) { return redirect()->back()->withInput($request->input())->with('fail', '添加车厂失败,数据库操作返回异常!'); } // if ($admin->id) { //添加管理员成功 // $garage->save(); // if($garage->id) { // 添加车厂(及管理员)成功 // return redirect()->route('admin.checkgarage.index')->with('message', '成功添加新车厂!'); // } // else { //添加失败 // return redirect()->back()->withInput($request->input())->with('fail', '添加管理员成功但添加车厂失败,数据库操作返回异常!'); // } // } // else { //添加管理员失败 // return redirect()->back()->withInput($request->input())->with('fail', '添加管理员失败,数据库操作返回异常!'); // } }