Laravel数据库发布请求中传递了错误的实例

时间:2014-08-05 17:56:05

标签: php laravel-4 eloquent

所以我正在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

这是我的 EloquentInviteRepository.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);
}

照亮\ 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();
}

如果这些有助于调试问题!

根据请求,我已经包含了post函数期间使用的控制器元素,这是激活存储库并提示错误的部分:

<?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 );
    });
  }
}

有什么想法,我错过了什么?

感谢帮助人员, 到目前为止,你一直是一个很好的资源!

1 个答案:

答案 0 :(得分:0)

在文件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');