Laravel 5 SQLSTATE [42S02]:找不到基表或视图 [英] Laravel 5 SQLSTATE[42S02]: Base table or view not found
问题描述
我正在研究Laravel中的存储库设计模式,并且正在使用 https://github.com /andersao/l5-存储库.
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]:找不到基表或视图:1146表 'test.nhanviens'不存在(SQL:从
nhanviens
选择*)
SQLSTATE[42S02]: Base table or view not found: 1146 Table 'test.nhanviens' doesn't exist (SQL: select * from
nhanviens
)
我数据库中的表不是Nhanvien
在我的代码中
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());
}
}
推荐答案
来自App \ Nhanvien.php,将此变量添加到类中:
from App\Nhanvien.php Add this variable to the class:
protected $table = 'nhanvien';
说明:除非明确指定其他名称,否则将使用蛇形"类的类的复数名称作为表名.因此,在这种情况下,Eloquent将假设nhanvien模型将记录存储在nhanviens表中.
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.
这篇关于Laravel 5 SQLSTATE [42S02]:找不到基表或视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!