sammy
sammy

Reputation: 11

Laravel 5.5: Dependency Injection on user model

I have a BaseRepository class with methods which will be valid for all Models. The only problem is, that the User model is derived from use Illuminate\Foundation\Auth\User. So it throws me a type error because the constructor requires an instance of Illuminate\Database\Eleoquent\Model. How can I solve the problem?

Here is my UserRepository.php:

namespace App\Repositories\User;

use App\Model\User; use App\Repositories\Base\BaseRepository;

class UserRepository extends BaseRepository {

public function __construct(User $user)
{
 parent::__construct($user);
}

}

BaseRepository.php

namespace App\Repositories\Base;

use App\User;
use Illuminate\Database\Eleoquent\Model;

class BaseRepository {

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

public function all() {

    return $this->model->orderBy('id','desc')->get();


}


}

This is the error: Type error: Argument 1 passed to App\Repositories\Base\BaseRepository::__construct() must be an instance of Illuminate\Database\Eleoquent\Model, instance of App\User given, called in C:\wamp64\www\adblog\app\Repositories\User\UserRepository.ph‌​p

Upvotes: 1

Views: 1049

Answers (1)

plmrlnsnts
plmrlnsnts

Reputation: 1684

Customize your User model like this

use Illuminate\Auth\Authenticatable;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Auth\Passwords\CanResetPassword;
use Illuminate\Foundation\Auth\Access\Authorizable;
use Illuminate\Contracts\Auth\Authenticatable as AuthenticatableContract;
use Illuminate\Contracts\Auth\Access\Authorizable as AuthorizableContract;
use Illuminate\Contracts\Auth\CanResetPassword as CanResetPasswordContract;

class User extends Model implements
    AuthenticatableContract,
    AuthorizableContract,
    CanResetPasswordContract
{
    use Authenticatable, Authorizable, CanResetPassword;
}

Upvotes: 2

Related Questions