2016-07-26 70 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 
相關問題