Laravel 5 SQLSTATE [42S02]:找不到基表或视图
问题描述:
我正在研究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());
}
}
答
如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';
,而不是如果你的表名是全小写。
答
就我而言,我已经通过执行命令
php artisan config:cache
您可以更新应用程序\实体\ Nhanvien摆脱类似的错误呢? – mydo47
public function model(){return“App \\ Nhanvien”; } – mydo47
我只是添加你的功能,但我的代码仍然不工作:) – jonny