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

2019-04-10 06:16发布

问题:

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:

Check your migration file, maybe you are using Schema::table, like this:

Schema::table('table_name', function ($table) { // ... });

If you want to create a new table you must use Schema::create:

 Schema::create('table_name', function ($table)  {
     // ... });

See the Laravel migration documentation for more information.

If you are using Schema::create then please provide the contents of your migration file.



回答4:

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

php artisan config:cache