Laravel 5 SQLSTATE[42S02]: Base table or view not found

匿名 (未验证) 提交于 2019-12-03 07:50:05

问题:

I'm studing about Repository Design Pattern in Laravel and I'm using https://github.com/andersao/l5-repository to do it.

I think i install success in my project . But when i run code with repository i have some problem

SQLSTATE[42S02]: Base table or view not found: 1146 Table 'test.nhanviens' doesn't exist (SQL: select * from nhanviens)

Table in my database is Nhanvien not Nhanviens

Here in my code

NhanvienRepository.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());     } } 

回答1:

from App\Nhanvien.php Add this variable to the class:

 protected $table = 'nhanvien'; 

Explanation: The "snake case", plural name of the class will be used as the table name unless another name is explicitly specified. So, in this case, Eloquent will assume the nhanvien model stores records in the nhanviens table.



回答2:

As stated in the official Eloquent documentation you need to specifically set the table name in your Model definition. That is, in your App\Nhanvien.php file set the following:

<?php  namespace App;  use Illuminate\Database\Eloquent\Model;  class Nhanvien extends Model {     /**      * The table associated with the model.      *      * @var string      */     protected $table = 'Nhanvien'; } 

or use

protected $table = 'nhanvien'; 

instead if your table name is full lowercase.



回答3:

In my case, I've got rid of similar error by executing the command

php artisan config:cache 


易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!