Laravel 5.5:用户模型上的依赖注入
我有一个BaseRepository类,它的方法对所有模型都有效。唯一的问题是,用户模型是从使用Illuminate \ Foundation \ Auth \ User派生而来的。所以它会抛出一个类型错误,因为构造函数需要一个Illuminate \ Database \ Eleoquent \ Model实例。我该如何解决这个问题?Laravel 5.5:用户模型上的依赖注入
这里是我的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();
}
}
这是错误:类型错误:传递给应用程序\库\基地\ BaseRepository 参数1: :__ construct()必须是一个Illuminate \ Database \ Eleoquent \ Model实例,该实例是在C:\ wamp64 \ www \ adblog \ app \ Repositories \ User \ UserRepository.ph中给出的App \ User的实例,p
回答:
自定义您的User
模型这样
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;
}
以上是 Laravel 5.5:用户模型上的依赖注入 的全部内容, 来源链接: utcz.com/qa/257294.html