首页 文章

Laravel PHP:Eloquent Repository无法识别模型

提问于
浏览
-1

我遇到了一个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);
   }
}

Model(app\models\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' .

Error Message that is displayed:

'Class acme\repositories\Video not found'

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

1 回答

  • 1

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

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

    namespace Acme\repositories\Eloquent;
    

    或者将下面的使用行添加到您的其他文件,但 I doubt that is what you want

    use \Video;
    

相关问题