I have a simple modal having an index method to get data from DB
Modal: Nation
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
class Nation extends Model{
function index(){
Nation::where(['visible'=>1])->get();
}
}
Now I want to call this function from a controller:
Controller:
$nations = Nation::index();
To make this I created a Facade in this way:
- Created a Provider
- Registered the provider in config/app.php
- Created the Facade
- Registered the Alias in config/app.php
Step 1 - Provider:
php artisan make:provider NationServiceProvider
public function register() {
$this->app->bind('nation',function(){
return new Nation();
});
}
Step 2: Register the provider in config/app.php
In providers array:
App\Providers\NationServiceProvider::class,
Step 3 Create the Facade
I created a folder App/Facades/ and inside the file NationFacade.php
namespace App\Facades;
use Illuminate\Support\Facades\Facade;
class NationFacade extends Facade{
protected static function getFacadeAccessor(){
return 'nation';
}
}
Step 4: Register the alias in config/app.php
In Aliases array:
'Nation' => App\Facades\NationFacade::class
But, when I run the controller I got the error:
"message": "Non-static method App\\Models\\Nation::index() should not be called statically",
I also tried to clear cache and composer dump. What I'm missing here?
Thanks for any sugegstion!
NationFacade::index()
. AFAIK, the call is directly happening fromNation
model which should not be happening. Call should happen in the facade and it should resolve the method and call it. – Cerlin