Laravel PHP:Eloquent Repository无法识别模型

时间:2014-11-07 15:48:59

标签: php laravel-4

我遇到了一个Eloquent Repository的问题,我已经创建了一个问题,因为它没有识别出我试图从我的表单中收集的数据的模型。我甚至将我的模型复制到Eloquent Repository的命名空间中,但它仍然没有被识别。

Eloquent Repository (app \ Acme \ repositories \ Eloquent \ EloquentVideoRepository.php):

<?php namespace Acme\repositories\Eloquent;

use acme\repositories\VideoRepository;

/* Tried copying the 'Video' model into the same
   namespace as this Eloquent Repository but it is not being recognized. */

use acme\repositories\Video;

class EloquentVideoRepository implements VideoRepository {

   public function all()
   {
      return Video::all();
   }

  /* This function below generates an error since the 'Video' class 
     is not found from my 'Video' Model */

   public function create($input)
   {
    return Video::create($input);
   }
}

模型(APP \模型\ Video.php):

<?php

 /* Original Line of code below that generated the error: 

 class Video extends Eloquent 
 */

/* EDIT:  I put a backslash in front of Eloquent
and now the model is recognized. 
*/
class Video extends \Eloquent {

/**
 * The table used by this model
 *
 * @var string
 **/
protected $table = 'videos';

/**
 * The primary key
 *
 * @var string
 **/
protected $primaryKey = 'video_id';

/**
 * The fields that are guarded cannot be mass assigned
 *
 * @var array
 **/
protected $guarded = array();

/**
*  Enabling soft deleting
*
*  @var boolean
**/
 protected $softDelete = true;

} 

我还将上面的模型复制到&app; \ acme \ repositories&#39;。

显示的错误消息:

'Class acme\repositories\Video not found'

即使将模型复制到此命名空间后,我也尝试过执行php artisan dump-autoload,但它仍无法正常工作。任何帮助表示赞赏。

1 个答案:

答案 0 :(得分:1)

Video类文件缺少任何名称空间声明,因此位于默认名称空间中。扩展某些内容并不会改变它的命名空间。

在顶部添加此行,将其添加到您想要的命名空间:

namespace Acme\repositories\Eloquent;

或者将下面的使用行添加到您的其他文件中,但我怀疑这是您想要的

use \Video;
相关问题