2016-07-26 20 views
3

私はLaravelのリポジトリデザインパターンについて掘り下げており、私はhttps://github.com/andersao/l5-repositoryを使っています。Laravel 5 SQLSTATE [42S02]:ベーステーブルまたはビューが見つかりません

私は自分のプロジェクトに成功したと思う。ベース・テーブルまたは見つからないビュー:1146表は 「test.nhanviens」は存在しません(SQL:nhanviensから選択*)を

私はリポジトリを使用してコードを実行したときに、私はいくつかの問題

SQLSTATE [42S02]を持っています

私のデータベース内のテーブルNhanvienはここに私のコードで

Nhanviensない

NhanvienRep

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

ository.php 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

を実行することにより、同様のエラーを取り除く持っていますか? – mydo47

+0

パブリック関数モデル(){return "App \\ Nhanvien"; } – mydo47

+0

私はちょうどあなたの関数を追加しましたが、私のコードはまだ動作しません:) – jonny

答えて

4

のApp \ Nhanvien.phpからクラスにこの変数を追加します。

protected $table = 'nhanvien'; 

説明:別の名前が明示的に指定されていない限り、「スネークケース」、複数のクラス名がテーブル名として使用されます。したがって、この場合、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

は、私はあなたがNhanvien \アプリケーション\エンティティを更新することができ、コマンドに

php artisan config:cache 
関連する問題