nicolaslopezj / searchable

A php trait to search laravel models
MIT License
2.01k stars 291 forks source link

Facade root not found #86

Open heinbez opened 8 years ago

heinbez commented 8 years ago

Hi there,

Great Library! I'm facing an issue issue though, I keep receiving the error "A facade root has not been set" as follow:

object(RuntimeException)#130 (7) {
  ["message":protected]=>
  string(31) "A facade root has not been set."
  ["string":"Exception":private]=>
  string(0) ""
  ["code":protected]=>
  int(0)
  ["file":protected]=>
  string(63) "/var/www/xxx/vendor/illuminate/support/Facades/Facade.php"
  ["line":protected]=>
  int(206)
  ["trace":"Exception":private]=>

Thanks in advance

wouterds commented 8 years ago

I'm getting the same error (using Eloquent as a standalone package).

porozhnyy commented 8 years ago

I got the same error when used alone...

heinbez commented 8 years ago

I solved it, hopefully this will help anyone that runs across the same error. You need to create a new app container and then bind it to the Facade.

use \Illuminate\Container\Container as Container;
use \Illuminate\Support\Facades\Facade as Facade;

/**
* Setup a new app instance container
* 
* @var Illuminate\Container\Container
*/
$app = new Container();
$app->singleton('app', 'Illuminate\Container\Container');

/**
* Set $app as FacadeApplication handler
*/
Facade::setFacadeApplication($app);
toby1991 commented 8 years ago

in lumen: bootstrap/app.php $app->withFacades();

taoongjun commented 7 years ago

But I don't konw how to create a new container. Could you help me.Thank you.

bingalls commented 7 years ago

@taoongjun Did you try the sample code from @heinbez ? This code is helpful for other Laravel tests, such as enabling the Log facade, in a static class (SUT) test target. Here's is more modern code, tested in a unit test, with Laravel v5.2:

use Illuminate\Foundation\Application; // ideally, near top of file use Illuminate\Support\Facades\Facade; //... $app = new Application(); // Create a facade root, perhaps in setup() $app->singleton('app', Application::class); Facade::setFacadeApplication($app);

DungLV99 commented 6 years ago

hello heinbez, my code show this error, you can help me fix it, tks PHP Fatal error: Uncaught ReflectionException: Class db does not exist in D:\xampp\htdocs\web\wordpress\laravel\vendor\laravel\framework\src\Illuminate\Container\Container.php:729

huanhuan0023 commented 6 years ago

I have the same problem hoping you can help me to solve

alessandro-c commented 6 years ago

The problem is that the trait forces you to use facades please see the example below :

protected function getDatabaseDriver() {
       $key = $this->connection ?: Config::get('database.default');
       return Config::get('database.connections.' . $key . '.driver');
}

wouldn't it be better to do something like 👇

protected function getDatabaseDriver() {
       $config = app('config');
       $key = $this->connection ?: $config->get('database.connections.' . $key . '.driver');
       return $config->get('database.connections.' . $key . '.driver');
}

Lumen's users for example might chose not to enable facades at all I don't think it's right to force consumers to use them.

zhitoo commented 4 years ago

wrong way

public function register()
    {
        $this->app->bind('Activity', function($app)
        {
             new Activity;
        });
    }

right way 👍

public function register()
    {
        $this->app->bind('Activity', function($app)
        {
             return new Activity;
        });
    }

---------------------------------- don't forget return

taoongjun commented 4 years ago

Thanks for your honesty! But It’s so long time ago, I can’t rember how it’s happening. Thanks again!

在 2019年9月19日,15:21,Hossein Shafiei notifications@github.com 写道:

wrong way

public function register() { $this->app->bind('Activity', function($app) { new Activity; }); } right way 👍

public function register() { $this->app->bind('Activity', function($app) { return new Activity; }); } ---------------------------------- don't forget return

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/nicolaslopezj/searchable/issues/86?email_source=notifications&email_token=AELZFVNTER3GVO2DVWOCRFLQKMR6JA5CNFSM4BULHTDKYY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGOD7CPRMQ#issuecomment-533002418, or mute the thread https://github.com/notifications/unsubscribe-auth/AELZFVJZRPHYSMUEHFP5XBLQKMR6JANCNFSM4BULHTDA.

wahyu-amaldi commented 1 year ago

I got the same error when used alone..