Laravel在创建模型时使用不正确的表名

时间:2017-03-07 22:38:35

标签: php mysql laravel

我使用Eloquent在我的Laravel应用中创建模型。我不确定我更改了什么,但是当我尝试创建AppUser的新实例时,我现在收到此错误:

Next exception 'Illuminate\Database\QueryException' with message 'SQLSTATE[42S02]: Base table or view not found: 1146 Table 'gonote.appusers' doesn't exist (SQL: select count(*) as aggregate from `appUsers` where `email` = user-4720@gonote.co)' in /Users/james/projects/GoNote/laravel/vendor/laravel/framework/src/Illuminate/Database/Connection.php:647
Stack trace:
  0 ./vendor/laravel/framework/src/Illuminate/Database/Connection.php(607): Illuminate\Database\Connection->runQueryCallback('select count(*)...', Array, Object(Closure))
  1 ./vendor/laravel/framework/src/Illuminate/Database/Connection.php(326): Illuminate\Database\Connection->run('select count(*)...', Array, Object(Closure))
  2 ./vendor/laravel/framework/src/Illuminate/Database/Query/Builder.php(1719): Illuminate\Database\Connection->select('select count(*)...', Array, false)
  3 ./vendor/laravel/framework/src/Illuminate/Database/Query/Builder.php(1703): Illuminate\Database\Query\Builder->runSelect()
  4 ./vendor/laravel/framework/src/Illuminate/Database/Query/Builder.php(2094): Illuminate\Database\Query\Builder->get(Array)
  5 ./vendor/laravel/framework/src/Illuminate/Database/Query/Builder.php(2022): Illuminate\Database\Query\Builder->aggregate('count', Array)
  6 ./vendor/laravel/framework/src/Illuminate/Validation/DatabasePresenceVerifier.php(55): Illuminate\Database\Query\Builder->count()
  7 ./vendor/laravel/framework/src/Illuminate/Validation/Concerns/ValidatesAttributes.php(614): Illuminate\Validation\DatabasePresenceVerifier->getCount('appUsers', 'email', 'user-4720@gonot...', NULL, NULL, Array)
  8 ./vendor/laravel/framework/src/Illuminate/Validation/Validator.php(338): Illuminate\Validation\Validator->validateUnique('email', 'user-4720@gonot...', Array, Object(Illuminate\Validation\Validator))
  9 ./vendor/laravel/framework/src/Illuminate/Validation/Validator.php(253): Illuminate\Validation\Validator->validateAttribute('email', 'unique:appUsers')
  10 ./vendor/laravel/framework/src/Illuminate/Validation/Validator.php(278): Illuminate\Validation\Validator->passes()
  11 ./app/Http/Controllers/Auth/RegisterController.php(25): Illuminate\Validation\Validator->fails()
  12 [internal function]: App\Http\Controllers\Auth\RegisterController->postRegister(Object(Illuminate\Http\Request))
  13 ./vendor/laravel/framework/src/Illuminate/Routing/Controller.php(55): call_user_func_array(Array, Array)
  14 ./vendor/laravel/framework/src/Illuminate/Routing/ControllerDispatcher.php(44): Illuminate\Routing\Controller->callAction('postRegister', Array)
  15 ./vendor/laravel/framework/src/Illuminate/Routing/Route.php(204): Illuminate\Routing\ControllerDispatcher->dispatch(Object(Illuminate\Routing\Route), Object(App\Http\Controllers\Auth\RegisterController), 'postRegister')
  16 ./vendor/laravel/framework/src/Illuminate/Routing/Route.php(160): Illuminate\Routing\Route->runController()
  17 ./vendor/laravel/framework/src/Illuminate/Routing/Router.php(559): Illuminate\Routing\Route->run()
  18 ./vendor/laravel/framework/src/Illuminate/Routing/Pipeline.php(30): Illuminate\Routing\Router->Illuminate\Routing\{closure}(Object(Illuminate\Http\Request))
  19 ./app/Http/Middleware/RedirectIfAuthenticated.php(24): Illuminate\Routing\Pipeline->Illuminate\Routing\{closure}(Object(Illuminate\Http\Request))
  20 ./vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(148): App\Http\Middleware\RedirectIfAuthenticated->handle(Object(Illuminate\Http\Request), Object(Closure))
  21 ./vendor/laravel/framework/src/Illuminate/Routing/Pipeline.php(53): Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}(Object(Illuminate\Http\Request))
  22 ./vendor/laravel/framework/src/Illuminate/Routing/Middleware/SubstituteBindings.php(41): Illuminate\Routing\Pipeline->Illuminate\Routing\{closure}(Object(Illuminate\Http\Request))
  23 ./vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(148): Illuminate\Routing\Middleware\SubstituteBindings->handle(Object(Illuminate\Http\Request), Object(Closure))
  24 ./vendor/laravel/framework/src/Illuminate/Routing/Pipeline.php(53): Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}(Object(Illuminate\Http\Request))
  25 ./vendor/laravel/framework/src/Illuminate/Routing/Middleware/ThrottleRequests.php(49): Illuminate\Routing\Pipeline->Illuminate\Routing\{closure}(Object(Illuminate\Http\Request))
  26 ./vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(148): Illuminate\Routing\Middleware\ThrottleRequests->handle(Object(Illuminate\Http\Request), Object(Closure), '60', '1')
  27 ./vendor/laravel/framework/src/Illuminate/Routing/Pipeline.php(53): Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}(Object(Illuminate\Http\Request))
  28 ./vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(102): Illuminate\Routing\Pipeline->Illuminate\Routing\{closure}(Object(Illuminate\Http\Request))
  29 ./vendor/laravel/framework/src/Illuminate/Routing/Router.php(561): Illuminate\Pipeline\Pipeline->then(Object(Closure))
  30 ./vendor/laravel/framework/src/Illuminate/Routing/Router.php(520): Illuminate\Routing\Router->runRouteWithinStack(Object(Illuminate\Routing\Route), Object(Illuminate\Http\Request))
  31 ./vendor/laravel/framework/src/Illuminate/Routing/Router.php(498): Illuminate\Routing\Router->dispatchToRoute(Object(Illuminate\Http\Request))
  32 ./vendor/laravel/framework/src/Illuminate/Foundation/Http/Kernel.php(174): Illuminate\Routing\Router->dispatch(Object(Illuminate\Http\Request))
  33 ./vendor/laravel/framework/src/Illuminate/Routing/Pipeline.php(30): Illuminate\Foundation\Http\Kernel->Illuminate\Foundation\Http\{closure}(Object(Illuminate\Http\Request))
  34 ./vendor/laravel/framework/src/Illuminate/Foundation/Http/Middleware/TransformsRequest.php(30): Illuminate\Routing\Pipeline->Illuminate\Routing\{closure}(Object(Illuminate\Http\Request))
  35 ./vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(148): Illuminate\Foundation\Http\Middleware\TransformsRequest->handle(Object(Illuminate\Http\Request), Object(Closure))
  36 ./vendor/laravel/framework/src/Illuminate/Routing/Pipeline.php(53): Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}(Object(Illuminate\Http\Request))
  37 ./vendor/laravel/framework/src/Illuminate/Foundation/Http/Middleware/TransformsRequest.php(30): Illuminate\Routing\Pipeline->Illuminate\Routing\{closure}(Object(Illuminate\Http\Request))
  38 ./vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(148): Illuminate\Foundation\Http\Middleware\TransformsRequest->handle(Object(Illuminate\Http\Request), Object(Closure))
  39 ./vendor/laravel/framework/src/Illuminate/Routing/Pipeline.php(53): Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}(Object(Illuminate\Http\Request))
  40 ./vendor/laravel/framework/src/Illuminate/Foundation/Http/Middleware/ValidatePostSize.php(27): Illuminate\Routing\Pipeline->Illuminate\Routing\{closure}(Object(Illuminate\Http\Request))
  41 ./vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(148): Illuminate\Foundation\Http\Middleware\ValidatePostSize->handle(Object(Illuminate\Http\Request), Object(Closure))
  42 ./vendor/laravel/framework/src/Illuminate/Routing/Pipeline.php(53): Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}(Object(Illuminate\Http\Request))
  43 ./vendor/laravel/framework/src/Illuminate/Foundation/Http/Middleware/CheckForMaintenanceMode.php(46): Illuminate\Routing\Pipeline->Illuminate\Routing\{closure}(Object(Illuminate\Http\Request))
  44 ./vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(148): Illuminate\Foundation\Http\Middleware\CheckForMaintenanceMode->handle(Object(Illuminate\Http\Request), Object(Closure))
  45 ./vendor/laravel/framework/src/Illuminate/Routing/Pipeline.php(53): Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}(Object(Illuminate\Http\Request))
  46 ./vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(102): Illuminate\Routing\Pipeline->Illuminate\Routing\{closure}(Object(Illuminate\Http\Request))
  47 ./vendor/laravel/framework/src/Illuminate/Foundation/Http/Kernel.php(149): Illuminate\Pipeline\Pipeline->then(Object(Closure))
  48 ./vendor/laravel/framework/src/Illuminate/Foundation/Http/Kernel.php(116): Illuminate\Foundation\Http\Kernel->sendRequestThroughRouter(Object(Illuminate\Http\Request))
  49 ./public/index.php(54): Illuminate\Foundation\Http\Kernel->handle(Object(Illuminate\Http\Request))
  50 ./server.php(21): require_once('/Users/james/pr...')
  51 {main}

查询失败是有道理的,因为该表的名称为app_users而不是appUsers。但是,我在所有查询中使用了Eloquent,因此我不会手动编写任何查询。我尝试将protected $table = 'app_users'添加到我的AppUser模型类中,但它似乎仍在查询错误的表格。

这是创建模型的函数:

protected function create(array $fields)
{
    return AppUser::create([
        'email' => $fields['email'],
        'name' => $fields['name'],
        'photo' => $fields['photo'],
        'phone' => $fields['phone'],
        'password' => bcrypt($fields['password'])
    ]);
}

模型/ AppUser.php

<?php

namespace App\Models;

use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Database\Eloquent\Model;
use Backpack\CRUD\CrudTrait;
use Laravel\Passport\HasApiTokens;

class AppUser extends Authenticatable
{
    use CrudTrait, HasApiTokens;

    protected $table = 'app_users';
    //protected $primaryKey = 'id';
    // public $timestamps = false;
    // protected $guarded = ['id'];
    protected $fillable = ['email', 'password', 'name', 'photo', 'phone', 'api_token'];
    protected $hidden = ['password'];
    // protected $dates = [];

    /*
    |--------------------------------------------------------------------------
    | FUNCTIONS
    |--------------------------------------------------------------------------
    */
    public function getAuthIdentifier() {
        return $this->getKey();
    }

    public function getAuthPassword() {
        return $this->password;
    }

    public function findForPassport($username) {
        return $this->where('email', $username)->first();
    }
}

2 个答案:

答案 0 :(得分:3)

看起来问题是由验证引起的 - 失败的查询似乎是唯一的验证器。检查您在电子邮件地址上进行唯一验证的位置,并正确传递表名。

答案 1 :(得分:2)

查看堆栈跟踪。您正在添加新用户,并且Validator符合email|unique:appUsers规则。你只需要在那里修复表名。

    return Validator::make($data, [
        'name' => 'required|max:255',
        'email' => 'required|email|max:255|unique:app_users', // here
        'password' => 'required|min:6|confirmed',
    ]);