/**
  * Run the database seeds.
  *
  * @return void
  */
 public function run()
 {
     Role::create(['name' => 'Administrator', 'priority' => 1, 'description' => 'Can do everything']);
     Role::create(['name' => 'Super User', 'priority' => 5, 'description' => 'Can moderate the forum and access the app']);
     Role::create(['name' => 'Moderator', 'priority' => 10, 'description' => 'Can moderate the forum']);
     Role::create(['name' => 'User', 'priority' => 15, 'description' => 'Can edit his profile']);
     Role::create(['name' => 'Angler', 'priority' => 20, 'description' => 'Can edit his profile']);
 }
 /**
  * Returns an array of User Roles
  *
  * @return array
  */
 public static function getUserRoles()
 {
     $roles = [];
     foreach (Role::all() as $role) {
         $roles[$role->id] = $role->name;
     }
     return $roles;
 }
 /**
  * Run the database seeds.
  *
  * @return void
  */
 public function run()
 {
     $faker = Faker\Factory::create();
     $roleIds = Role::lists('id')->toArray();
     $permissionIds = Permission::lists('id')->toArray();
     foreach ($roleIds as $roleId) {
         $permissions = $faker->randomElements($permissionIds, $faker->numberBetween(1, count($permissionIds)));
         // Skip rest of the loop if $categories empty
         if (empty($permissions)) {
             continue;
         }
         foreach ($permissions as $permissionId) {
             DB::table('users_pivot_permission_role')->insert(['role_id' => $roleId, 'permission_id' => $permissionId]);
         }
     }
 }
 /**
  * Update the specified resource in storage.
  *
  * @param UserRequest|Request $request
  * @param  int $id
  * @return \Illuminate\Http\Response
  */
 public function update(UserRequest $request, $id)
 {
     // Get only User related input fields
     $userFields = Input::only('first_name', 'last_name', 'phone_number', 'email', 'password', 'date_of_birth');
     // Hash the password
     if (!empty($userFields['password'])) {
         $userFields['password'] = bcrypt($request['password']);
     }
     // Get only Address
     $addressFields = Input::only('address')['address'];
     // If any exceptions are thrown any DB operations
     // will be automatically rolled back.
     DB::transaction(function () use($request, $userFields, $addressFields, $id) {
         // Fetch User from the database
         $user = User::findOrFail($id);
         // Update user details
         foreach ($userFields as $k => $v) {
             if (!($k == 'password' && empty($v))) {
                 $user->{$k} = $v;
             }
         }
         // Create new Address if does not exist in the database
         if (is_null($user->address)) {
             // Create new Address if input fields not empty.
             if (!empty($addressFields)) {
                 $user->address()->create($addressFields);
             }
         } else {
             // Update Address
             foreach ($addressFields as $k => $v) {
                 $user->address->{$k} = $v;
             }
             $user->address->save();
         }
         // Create new Avatar
         if (Input::hasFile('avatar') && Input::file('avatar')->isValid()) {
             Avatar::make($user, $request);
         }
         // Assign user to a Local Club
         $user->club()->associate(Club::find($request['club_id']));
         // Assign User to a Role
         $user->role()->associate(Role::find($request['role_id']));
         // Remove user categories
         if (is_null($request['categories'])) {
             $user->categories()->detach();
         } else {
             if (is_array($request['categories'])) {
                 // Assign User to categories
                 $user->categories()->sync($request['categories']);
             }
         }
         // Save user
         $user->save();
     });
     flash()->success('Success!', "User data has been updated!");
     return redirect(route('admin.users.show', $id));
 }