所以我正在Laravel中开发一个页面,该页面在提交电子邮件时生成邀请代码。我遇到了这个问题,每次当我将电子邮件输入表单时,它都应该生成一个邀请代码并将其输入到我的数据库中,然后重定向我。相反,我得到了这个错误代码:
Argument 1 passed to myapp'Repositories'Invite'EloquentInviteRepository::__construct()
must be an instance of Illuminate'Database'Eloquent'Model, instance of
Illuminate'Foundation'Application given, called in /var/www/laravel/bootstrap/compiled.php
on line 4259 and defined
这是我的EloquentInviteDepository.php文件,显然第21行是错误的一行:
<?php namespace myapp'Repositories'Invite;
use myapp'Repositories'Crudable;
use Illuminate'Support'MessageBag;
use myapp'Repositories'Repository;
use Illuminate'Database'Eloquent'Model;
use myapp'Repositories'AbstractRepository;
class EloquentInviteRepository extends AbstractRepository implements Repository, Crudable, InviteRepository {
/**
* @var Illuminate'Database'Eloquent'Model
*/
protected $model;
/**
* Construct
*
* @param Illuminate'Database'Eloquent'Model $user
*/
public function __construct(Model $model)
{
parent::__construct(new MessageBag);
$this->model = $model;
}
/**
* Find a valid invite by a code
*
* @param string $code
* @return Illuminate'Database'Eloquent'Model
*/
public function getValidInviteByCode($code)
{
return $this->model->where('code', '=', $code)
->where('claimed_at', '=', null)
->first();
}
/**
* Create
*
* @param array $data
* @return Illuminate'Database'Eloquent'Model
*/
public function create(array $data)
{
$data['code'] = bin2hex(openssl_random_pseudo_bytes(16));
return $this->model->create($data);
}
/**
* Update
*
* @param array $data
* @return Illuminate'Database'Eloquent'Model
*/
public function update(array $data){}
/**
* Delete
*
* @param int $id
* @return boolean
*/
public function delete($id){}
}
以防有人好奇;Illuminate''Database''Eloquent''Model.php的__construct()
接口:
/**
* Create a new Eloquent model instance.
*
* @param array $attributes
* @return void
*/
public function __construct(array $attributes = array())
{
$this->bootIfNotBooted();
$this->syncOriginal();
$this->fill($attributes);
}
和Illuminate''Foundation''Application.php:
/**
* Create a new Illuminate application instance.
*
* @param 'Illuminate'Http'Request $request
* @return void
*/
public function __construct(Request $request = null)
{
$this->registerBaseBindings($request ?: $this->createNewRequest());
$this->registerBaseServiceProviders();
$this->registerBaseMiddlewares();
}
万一这些有助于调试问题!
根据请求,我已经包含了在发布功能期间使用的控制器元素,这似乎是激活存储库并提示错误的部分:
<?php
use myapp'Repositories'Invite'InviteRepository;
class InviteController extends BaseController {
/**
* InviteRepository
*
* @var myapp'Repositories'Invite'InviteRepository
*/
protected $repository;
/**
* Create a new instance of the InviteController
*
* @param myapp'Repositories'Invite'InviteRepository
*/
public function __construct(InviteRepository $repository)
{
$this->repository = $repository;
}
/**
* Create a new invite
*
* @return Response
*/
public function store()
{
$invite = $this->repository->create(Input::all());
}
}
RepositoryServiceProvider.php
<?php namespace myapp'Repositories;
use Illuminate'Support'ServiceProvider;
class RepositoryServiceProvider extends ServiceProvider {
/**
* Register
*/
public function register()
{
$this->registerInviteRepository();
}
/**
* Register the Invite Repository
*
* @return void
*/
public function registerInviteRepository()
{
$this->app->bind('myapp'Repositories'Invite'InviteRepository', function($app)
{
return new EloquentInviteRepository( new Invite );
});
}
}
你知道我缺了什么吗?
谢谢你们的帮助,伙计们,到目前为止,你一直是一个很好的资源!
在文件RepositoryServiceProvider.php
中,替换此
$this->app->bind('myapp'Repositories'Invite'InviteRepository', function($app)
{
return new EloquentInviteRepository( new Invite );
});
有了这个:
$this->app->bind('myapp'Repositories'Invite'InviteRepository',
'myapp'Repositories'Invite'EloquentInviteRepository');