Search code examples
laraveleloquentlumeninstantiation

Target [Illuminate\Database\Eloquent\Model] is not instantiable while building


Peeps, I'm lost. Tried everything and after 5 hours of searching through the 10th page of Google hits, I give up. Maybe I just dont know how to ask Google the correct keywords..

I have this scenario: In lumen app, lets call it X, I have require custom packages CRUD and Storage, Storage is using functionality of CRUD.

StorageService has:

use Crud\Services\BaseService;
class StorageService extends BaseService{}

And Crud\BaseService has constructor, that uses Model:

use Illuminate\Database\Eloquent\Model;
class BaseService
{
    protected $model;

    public function __construct(Model $model)
    {
        $this->model = $model;
    }
}

When I try to do anything with my app X, I get error:

Target [Illuminate\Database\Eloquent\Model] is not instantiable while building [Lumee\Storage\Services\StorageService]

I cannot get my head around how to get to proper class of Model, since I saw, that Model is abstract class.

Also, I'm using this CRUD package successfully in another App, only difference is, there CRUD is used directly in app, not via some other package. I'm confused, why there is working without any additional bindings and service registering..

EDIT: Added some binding into StorageServiceProvider (boot and register methods):

        $this->app->bind(BaseService::class, function(){
        return new BaseService(new Model());
    });

And registered StorageServiceProvider in my boostrap/app.php:

$app->register(Storage\Providers\StorageServiceProvider::class);

Thing still returns same error. I tried with binding in CrudServiceProvider, nope.


Solution

  • you can't get object from abstract class (Model class) to solve this try this :

    use Illuminate\Database\Eloquent\Model;
    class BaseService
    {
        protected $model;
    
    }
    

    suppose your model is (Storage) :

    use Crud\Services\BaseService;
    class StorageService extends BaseService{
    
            public function __construct(Storage $model)
            {
                $this->model = $model;
            }
    }