2016-07-26 325 views
3

我正在研究Laravel中的Repository Design Pattern,我正在使用https://github.com/andersao/l5-repository来完成它。Laravel 5 SQLSTATE [42S02]:找不到基表或视图

我想我在我的项目中安装成功。但是,当我与存储库运行代码我有一些问题

SQLSTATE [42S02]:基表或视图未找到:1146表 “test.nhanviens”不存在(SQL:从nhanviens选择*)

表在我的数据库是Nhanvien不Nhanviens

在这里,在我的代码

NhanvienRep ository.php

<?php 

    namespace App\Repositories; 

    use Prettus\Repository\Contracts\RepositoryInterface; 

    /** 
    * Interface NhanvienRepository 
    * @package namespace App\Repositories; 
    */ 
    interface NhanvienRepository extends RepositoryInterface 
    { 
     // 
    } 

NhanvienRepositoryEloquent.php

<?php 

namespace App\Repositories; 

use Prettus\Repository\Eloquent\BaseRepository; 
use Prettus\Repository\Criteria\RequestCriteria; 
use App\Repositories\NhanvienRepository; 
use App\Entities\Nhanvien; 
use App\Validators\NhanvienValidator; 

/** 
* Class NhanvienRepositoryEloquent 
* @package namespace App\Repositories; 
*/ 
class NhanvienRepositoryEloquent extends BaseRepository implements NhanvienRepository 
{ 
    /** 
    * Specify Model class name 
    * 
    * @return string 
    */ 
    public function model() 
    { 
     return Nhanvien::class; 
    } 



    /** 
    * Boot up the repository, pushing criteria 
    */ 
    public function boot() 
    { 
     $this->pushCriteria(app(RequestCriteria::class)); 
    } 
} 

DataController.php

<?php 

namespace App\Http\Controllers; 

use Illuminate\Http\Request; 

use App\Http\Requests; 
use App\nhanvien; 
use App\Repositories\NhanvienRepository; 

class DataController extends Controller 
{ 
    protected $repository; 

    public function __construct(NhanvienRepository $repository){ 
     $this->repository = $repository; 
    } 

    public function DanhSach(){ 
     var_dump($this->repository->all()); 
    } 
} 
+0

您可以更新应用程序\实体\ Nhanvien摆脱类似的错误呢? – mydo47

+0

public function model(){return“App \\ Nhanvien”; } – mydo47

+0

我只是添加你的功能,但我的代码仍然不工作:) – jonny

回答

4

从应用程序\ Nhanvien.php这个变量添加到类:

protected $table = 'nhanvien'; 

说明:除非明确指定另一个名称,否则将使用该类的复数名称“snake case”作为表名称。因此,在这种情况下,Eloquent将假设nhanvien模型将记录存储在nhanviens表中。

+0

谢谢。但在我的模型中,我有你的代码。我得到了同样的错误,我认为这不正确。 :) – jonny

+0

你的模型类扩展了什么?雄辩,模范还是热心? – ClearBoth

+0

我的模型扩展模型:D – jonny

1

official Eloquent documentation中所述,您需要在模型定义中专门设置表名。也就是说,在设置以下你的App \ Nhanvien.php文件:

<?php 

namespace App; 

use Illuminate\Database\Eloquent\Model; 

class Nhanvien extends Model 
{ 
    /** 
    * The table associated with the model. 
    * 
    * @var string 
    */ 
    protected $table = 'Nhanvien'; 
} 

或使用

protected $table = 'nhanvien'; 

,而不是如果你的表名是全小写。

+0

是我在数据库中的表格一切都是小写的 – jonny

+0

当你在线请帮助我:( – jonny

-1

就我而言,我已经通过执行命令

php artisan config:cache 
相关问题