gpt4 book ai didi

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

转载 作者:可可西里 更新时间:2023-10-31 22:52:02 26 4
gpt4 key购买 nike

我正在研究 Laravel 中的存储库设计模式,我正在使用 https://github.com/andersao/l5-repository去做。

我想我在我的项目中安装成功了。但是当我使用存储库运行代码时,我遇到了一些问题

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

我数据库中的表是 Nhanvien 而不是 Nhanviens

在我的代码中

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 将这个变量添加到类中:

 protected $table = 'nhanvien';

解释:“蛇形”,类的复数名称将用作表名,除非明确指定另一个名称。因此,在这种情况下,Eloquent 将假设 nhanvien 模型将记录存储在 nhanviens 表中。

关于php - Laravel 5 SQLSTATE[42S02] : Base table or view not found,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38581645/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com