1

I have installed Sentry with Composer, thus it has installed Illuminate Database too. The installation was successful, I have done everything in the Sentry documentation. I'm trying to add a user to the database with a simple code. However it gives me this error message:

Fatal error: Call to a member function connection() on a non-object in C:\Program Files\EasyPHP-DevServer-14.1VC9\data\localweb\ilhan\vendor\illuminate\database\Illuminate\Database\Eloquent\Model.php on line 2472

My code is as follows:

<?php
include_once "vendor/autoload.php";


use Illuminate\Database\Capsule\Manager as Capsule;
$capsule = new Capsule;

$capsule->addConnection([
    'driver'    => 'mysql',
    'host'      => 'localhost',
    'database'  => 'ilhantestdb',
    'username'  => 'root',
    'password'  => '',
    'charset'   => 'utf8',
    'collation' => 'utf8_unicode_ci',
]);



use Cartalyst\Sentry\Sentry as Sentry;


try
{
    $user = new Sentry;
    // Create the user
    $user->createUser(array(
        'email'     => 'john.doe@example.com',
        'password'  => 'test',
        'activated' => true,
    ));

    // Find the group using the group id
    $adminGroup = Sentry::findGroupById(1);

    // Assign the group to the user
    $user->addGroup($adminGroup);
}
catch (Cartalyst\Sentry\Users\LoginRequiredException $e)
{
    echo 'Login field is required.';
}
catch (Cartalyst\Sentry\Users\PasswordRequiredException $e)
{
    echo 'Password field is required.';
}
catch (Cartalyst\Sentry\Users\UserExistsException $e)
{
    echo 'User with this login already exists.';
}
catch (Cartalyst\Sentry\Groups\GroupNotFoundException $e)
{
    echo 'Group was not found.';
}

Even I don't know how to debug this. Also, I thought that since Illuminate comes with Sentry, Sentry should be coded how to handle Illuminate thus I don't need much configuration. The documentation is poor and I was unable to find what to do with this error.

rchaff
  • 9
  • 1
  • 1
  • 2
ilhan
  • 8,700
  • 35
  • 117
  • 201

2 Answers2

4

You must also boot the ORM. Documentation is not really clear about this.

use Illuminate\Database\Capsule\Manager as Capsule;
$capsule = new Capsule;

$capsule->addConnection([
     ...
]);

$capsule->bootEloquent();
Mika Tuupola
  • 19,877
  • 5
  • 42
  • 49
1

Try setting Capsule as a global. I can't speak to Sentry, but I'm trying to use Capsule on a project, myself, and am running into this very same issue.

Take a look here: https://github.com/illuminate/database/blob/master/Capsule/Manager.php#L113 You'll see that a lot of the convenience functions in there depend on having the static::$instance variable set, which gets set on https://github.com/illuminate/database/blob/master/Capsule/Manager.php#L192.

In my case, I'm trying to use $capsule without setting it as a global. What I ended needing to do was to write my query like so $capsule->getConnection()->table('foo')->get(). In your case, I think Sentry is attempting to access Eloquent and Capsule through static class methods.

tl;dr Run $capsule->setAsGlobal();

Michael Cordingley
  • 1,485
  • 1
  • 11
  • 23