Tuesday, March 7, 2017

Laravel using incorrect table name when creating a model

I'm using Eloquent to create models in my Laravel app. I'm not sure what I changed, but now I'm receiving this error when I try to create new instances of 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}

It makes sense that the query fails because the table is named app_users not appUsers. However, I'm using Eloquent for all of my queries, so I'm not writing any by hand. I've tried adding protected $table = 'app_users' to my AppUser model class, but it still appears to be querying the wrong table.

This is the function creating the model:

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



via SimpleJ

Advertisement