querta
querta

Reputation: 37

Interface is not instantiable while building Controller in Laravel

I am beginner in php and Laravel. I have project in Laravel 6. I make my project with this article: https://itnext.io/repository-design-pattern-done-right-in-laravel-d177b5fa75d4 I have in my serwer php 7.4

I have this code:

interface RepositoryInterface
{
    public function all(array $columns = ['*']);

    public function list(string $orderByColumn, string $orderBy = 'desc', array $with = [], array $columns = ['*']);

    public function listWithPaginate(string $orderByColumn, string $orderBy = 'desc', array $with = [], array $columns = ['*']);

    public function create(array $data);

    public function update(array $data, int $id, string $attribute = 'id');

    public function delete(int $id);

    public function find(int $id, array $columns = array('*'));

    public function getModel();

    public function findOrFail(int $id, array $columns = array('*'));
}

interface SlideRepositoryInterface extends RepositoryInterface
{
    public function search(string $query, string $orderByColumn, string $orderBy = 'desc', array $with = [], array $columns = ['*'], int $perPage = 10);
}



use App\Models\Slide;
use App\Repositories\Interfaces\SlideRepositoryInterface;
use Cache;

class SlideRepository implements SlideRepositoryInterface
{

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

    public function search(string $query, string $orderByColumn, string $orderBy = 'desc', array $with = [], array $columns = ['*'], int $perPage = 10)
    {
        return $this->model->where('line1', 'LIKE', '%' . $query . '%')->orWhere('id', 'LIKE', '%' . $query . '%')->with($with)->orderBy($orderByColumn, $orderBy)->paginate($perPage, $columns)->appends(request()->query());
    }
}




class SlideServiceProvider extends ServiceProvider
{
    public function register()
    {
        $this->app->bind(
            SlideRepositoryInterface::class,
            SlideRepository::class
        );
    }
}

And my controller:

public function __construct(SlideRepositoryInterface $repository, Request $request)
    {
        $this->model = $repository;
        $this->sortBy = $this->sortResultsBy($request->input('sortAfter') ?? 0, 'line1');
    }

abstract class BaseRepository implements RepositoryInterface
{

    protected $model;

    public function all(array $columns = array('*'))
    {
        return $this->model->get($columns);
    }

    public function list(string $orderByColumn, string $orderBy = 'desc', array $with = [], array $columns = ['*'])
    {
        return $this->model->with($with)
            ->orderBy($orderByColumn, $orderBy)
            ->get($columns);
    }

    public function listWithPaginate(string $orderByColumn, string $orderBy = 'desc', array $with = [], array $columns = ['*'], int $perPage = 10)
    {
        return $this->model->with($with)
            ->orderBy($orderByColumn, $orderBy)
            ->paginate($perPage, $columns)->appends(request()->query());
    }

    public function create(array $data): int
    {
        return $this->model->create($data)->id;
    }

    public function update(array $data, int $id, string $attribute = 'id'): void
    {
        $this->model->where($attribute, '=', $id)->update($data);
    }

    public function delete(int $id): void
    {
        $this->model->destroy($id);
    }

    public function find(int $id, array $columns = array('*'))
    {
        return $this->model->find($id, $columns);
    }

    public function getModel()
    {
        return $this->model;
    }

    public function getFirst(int $id)
    {
        return $this->model->where('id', $id)->first();
    }

    public function findOrFail(int $id, array $columns = array('*'))
    {
        return $this->model->findOrFail($id, $columns);
    }


}

When I run this code I have error:

Target [App\Repositories\Interfaces\SlideRepositoryInterface] is not instantiable while building [App\Http\Controllers\psCMS\SlideController]

I want make my project in Repository design pattern.

Why it's not working? How can I repair my project?

Please, help me :)

Upvotes: 0

Views: 5822

Answers (2)

Udechukwu
Udechukwu

Reputation: 41

Step 1 - add your service provider to config/app.php inside the providers array block:

   'providers' => ServiceProvider::defaultProviders()->merge([
App\Providers\SlideServiceProvider::class,
    ])

And after that, you can attempt these steps to ensure you don't have a cached problem:

php artisan cache:clear
php artisan config:clear

Upvotes: 0

mrhn
mrhn

Reputation: 18926

I believe you have not registred your service provider. Do that in config/app.php.

'providers' => [
    // Other Service Providers

    SlideServiceProvider ::class,
],

Your class does not implement the full interface, all of the interfaces methods needs to be implemented. IDE's can often auto generate these.

Upvotes: 4

Related Questions