Question

I have a Laravel 4 model:

Class Organisation extends Eloquent {

....


/**
 * add user to organisation & assign permission group
 *
 * @param $user User user to add to organisation
 * @param $group String name of the permissions group to add the user to
 * @return void
 */
public function addUser( User $user, $group = 'Editors' )
{
    // make sure the user is not already in the organisation
    if( ! $this->users->contains($user->id) ) 
    {
        // if organisation has no users, force first user as an admin
        if( ! $this->users->count() ) 
            $group = 'Admins';

        if( $group = $this->getGroup( $group ) )
        {
            if( $user->addGroup($group) && $this->users()->attach($user) )
                return true;
        }                  
    }
    else
        throw new UserExistsException( $user->fullName()." already belongs to ".$this->title.".");
}

.... 
}

Also i have a controller calling this function / model:

/**
 * Manage users form processing 
 *
 * @return Redirect
 */
public function postIndex( Organisation $organisation )
{ 
    if( $user = Sentry::findUserByLogin(Input::get('email')) ) 
    {
        try
        {
            if( $organisation->addUser( $user, 'Editors' ) )
                return Redirect::route('organisation-user-index', $organisation->id)
                    ->with('success', '<strong>' . $user->fullName() . '</strong> successfully added.');
        }
        catch(Cartalyst\Sentry\Users\UserExistsException $e)
        {
            return Redirect::route('organisation-user-index', $organisation->id)
                ->with('error', $e);
        }
    }

    return Redirect::route('organisation-user-index', $organisation->id)
        ->with('error', 'Something went wrong. Try again.');
}   

WHY is the catch statment not getting getting the exception when it happens? rather its simply getting thrown... and not caught?

Était-ce utile?

La solution

You're not showing all of your code, but it looks that you're using namespaces, so you probably will have to :

catch(\Cartalyst\Sentry\Users\UserExistsException $e)

Instead of

catch(Cartalyst\Sentry\Users\UserExistsException $e)
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top