我正在使用Laravel 5并且有多个模型(患者,治疗师)从用户模型扩展.
我在DB中有一些虚拟数据(很少有治疗师,很少有患者).
当我想通过id检索治疗师时,我尝试通过Therapist :: find($id)这样做,但得到一个例外:
'Illuminate\Database\QueryException' with message 'SQLSTATE[23000]:
Integrity constraint violation: 1062 Duplicate entry '' for key
'users_email_unique' (SQL: insert into users`
几个问题:
1.为什么当我使用:: find函数时,它会尝试插入一个新用户?
2.如何在从User扩展的对象上使用:: find函数以获取每个模型的属性?
编辑
Ajax调用
/getTherapist
routes.php文件:
Route::get('/getTherapist', 'HomeController@getTherapist');
HomeController.php:
public function getTherapist()
{
$user = Therapist::find(13);
return $user->toArray();
}
完全例外:
> Next exception 'Illuminate\Database\QueryException' with message
> 'SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry
> '' for key 'users_email_unique' (SQL: insert into `users`
> (`updated_at`, `created_at`) values (2016-07-24 13:26:42, 2016-07-24
> 13:26:42))' in
> C:\wamp\www\clinic\vendor\laravel\framework\src\Illuminate\Database\Connection.php:725
> Stack trace:
> #0 C:\wamp\www\clinic\vendor\laravel\framework\src\Illuminate\Database\Connection.php(681):
> Illuminate\Database\Connection->runQueryCallback('insert into `us...',
> Array, Object(Closure))
> #1 C:\wamp\www\clinic\vendor\laravel\framework\src\Illuminate\Database\Connection.php(454):
> Illuminate\Database\Connection->run('insert into `us...', Array,
> Object(Closure))
> #2 C:\wamp\www\clinic\vendor\laravel\framework\src\Illuminate\Database\Connection.php(410):
> Illuminate\Database\Connection->statement('insert into `us...', Array)
> #3 C:\wamp\www\clinic\vendor\laravel\framework\src\Illuminate\Database\Query\Processors\Processor.php(32):
> Illuminate\Database\Connection->insert('insert into `us...', Array)
> #4 C:\wamp\www\clinic\vendor\laravel\framework\src\Illuminate\Database\Query\Builder.php(2055):
> Illuminate\Database\Query\Processors\Processor->processInsertGetId(Object(Illuminate\Database\Query\Builder),
> 'insert into `us...', Array, 'id')
> #5 [internal function]: Illuminate\Database\Query\Builder->insertGetId(Array, 'id')
> #6 C:\wamp\www\clinic\vendor\laravel\framework\src\Illuminate\Database\Eloquent\Builder.php(1422):
> call_user_func_array(Array, Array)
> #7 C:\wamp\www\clinic\vendor\laravel\framework\src\Illuminate\Database\Eloquent\Model.php(1628):
> Illuminate\Database\Eloquent\Builder->__call('insertGetId', Array)
> #8 C:\wamp\www\clinic\vendor\laravel\framework\src\Illuminate\Database\Eloquent\Model.php(1628):
> Illuminate\Database\Eloquent\Builder->insertGetId(Array, 'id')
> #9 C:\wamp\www\clinic\vendor\laravel\framework\src\Illuminate\Database\Eloquent\Model.php(1597):
> Illuminate\Database\Eloquent\Model->insertAndSetId(Object(Illuminate\Database\Eloquent\Builder),
> Array)
> #10 C:\wamp\www\clinic\vendor\laravel\framework\src\Illuminate\Database\Eloquent\Model.php(1488):
> Illuminate\Database\Eloquent\Model->performInsert(Object(Illuminate\Database\Eloquent\Builder),
> Array)
> #11 C:\wamp\www\clinic\vendor\zizaco\entrust\src\Entrust\Traits\EntrustUserTrait.php(28):
> Illuminate\Database\Eloquent\Model->save(Array)
> #12 C:\wamp\www\clinic\app\Therapist.php(16): App\User->save()
> #13 C:\wamp\www\clinic\vendor\laravel\framework\src\Illuminate\Database\Eloquent\Model.php(538):
> App\Therapist->__construct()
> #14 C:\wamp\www\clinic\vendor\laravel\framework\src\Illuminate\Database\Eloquent\Builder.php(628):
> Illuminate\Database\Eloquent\Model::hydrate(Array, NULL)
> #15 C:\wamp\www\clinic\vendor\laravel\framework\src\Illuminate\Database\Eloquent\Builder.php(316):
> Illuminate\Database\Eloquent\Builder->getModels(Array)
> #16 C:\wamp\www\clinic\vendor\laravel\framework\src\Illuminate\Database\Eloquent\Builder.php(286):
> Illuminate\Database\Eloquent\Builder->get(Array)
> #17 C:\wamp\www\clinic\vendor\laravel\framework\src\Illuminate\Database\Eloquent\Builder.php(167):
> Illuminate\Database\Eloquent\Builder->first(Array)
> #18 [internal function]: Illuminate\Database\Eloquent\Builder->find(13)
> #19 C:\wamp\www\clinic\vendor\laravel\framework\src\Illuminate\Database\Eloquent\Model.php(3505):
> call_user_func_array(Array, Array)
> #20 [internal function]: Illuminate\Database\Eloquent\Model->__call('find', Array)
> #21 [internal function]: App\Therapist->find(13)
> #22 C:\wamp\www\clinic\vendor\laravel\framework\src\Illuminate\Database\Eloquent\Model.php(3519):
> call_user_func_array(Array, Array)
> #23 C:\wamp\www\clinic\app\Http\Controllers\HomeController.php(93): Illuminate\Database\Eloquent\Model::__callStatic('find', Array)
> #24 C:\wamp\www\clinic\app\Http\Controllers\HomeController.php(93): App\Therapist::find(13)
> #25 [internal function]: App\Http\Controllers\HomeController->getTherapist()
> #26 C:\wamp\www\clinic\vendor\laravel\framework\src\Illuminate\Routing\Controller.php(80):
> call_user_func_array(Array, Array)
> #27 C:\wamp\www\clinic\vendor\laravel\framework\src\Illuminate\Routing\ControllerDispatcher.php(146):
> Illuminate\Routing\Controller->callAction('getTherapist', Array)
> #28 C:\wamp\www\clinic\vendor\laravel\framework\src\Illuminate\Routing\ControllerDispatcher.php(94):
> Illuminate\Routing\ControllerDispatcher->call(Object(App\Http\Controllers\HomeController),
> Object(Illuminate\Routing\Route), 'getTherapist')
> #29 [internal function]: Illuminate\Routing\ControllerDispatcher->Illuminate\Routing\{closure}(Object(Illuminate\Http\Request))
> #30 C:\wamp\www\clinic\vendor\laravel\framework\src\Illuminate\Routing\Pipeline.php(52):
> call_user_func(Object(Closure), Object(Illuminate\Http\Request))
> #31 C:\wamp\www\clinic\app\Http\Middleware\Authenticate.php(28): Illuminate\Routing\Pipeline->Illuminate\Routing\{closure}(Object(Illuminate\Http\Request))
> #32 [internal function]: App\Http\Middleware\Authenticate->handle(Object(Illuminate\Http\Request),
> Object(Closure))
> #33 C:\wamp\www\clinic\vendor\laravel\framework\src\Illuminate\Pipeline\Pipeline.php(136):
> call_user_func_array(Array, Array)
> #34 [internal function]: Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}(Object(Illuminate\Http\Request))
> #35 C:\wamp\www\clinic\vendor\laravel\framework\src\Illuminate\Routing\Pipeline.php(32):
> call_user_func(Object(Closure), Object(Illuminate\Http\Request))
> #36 [internal function]: Illuminate\Routing\Pipeline->Illuminate\Routing\{closure}(Object(Illuminate\Http\Request))
> #37 C:\wamp\www\clinic\vendor\laravel\framework\src\Illuminate\Pipeline\Pipeline.php(103):
> call_user_func(Object(Closure), Object(Illuminate\Http\Request))
> #38 C:\wamp\www\clinic\vendor\laravel\framework\src\Illuminate\Routing\ControllerDispatcher.php(96):
> Illuminate\Pipeline\Pipeline->then(Object(Closure))
> #39 C:\wamp\www\clinic\vendor\laravel\framework\src\Illuminate\Routing\ControllerDispatcher.php(54):
> Illuminate\Routing\ControllerDispatcher->callWithinStack(Object(App\Http\Controllers\HomeController),
> Object(Illuminate\Routing\Route), Object(Illuminate\Http\Request),
> 'getTherapist')
> #40 C:\wamp\www\clinic\vendor\laravel\framework\src\Illuminate\Routing\Route.php(174):
> Illuminate\Routing\ControllerDispatcher->dispatch(Object(Illuminate\Routing\Route),
> Object(Illuminate\Http\Request), 'App\\Http\\Contro...',
> 'getTherapist')
> #41 C:\wamp\www\clinic\vendor\laravel\framework\src\Illuminate\Routing\Route.php(140):
> Illuminate\Routing\Route->runController(Object(Illuminate\Http\Request))
> #42 C:\wamp\www\clinic\vendor\laravel\framework\src\Illuminate\Routing\Router.php(724):
> Illuminate\Routing\Route->run(Object(Illuminate\Http\Request))
> #43 [internal function]: Illuminate\Routing\Router->Illuminate\Routing\{closure}(Object(Illuminate\Http\Request))
> #44 C:\wamp\www\clinic\vendor\laravel\framework\src\Illuminate\Routing\Pipeline.php(52):
> call_user_func(Object(Closure), Object(Illuminate\Http\Request))
> #45 C:\wamp\www\clinic\vendor\laravel\framework\src\Illuminate\Foundation\Http\Middleware\VerifyCsrfToken.php(64):
> Illuminate\Routing\Pipeline->Illuminate\Routing\{closure}(Object(Illuminate\Http\Request))
> #46 [internal function]: Illuminate\Foundation\Http\Middleware\VerifyCsrfToken->handle(Object(Illuminate\Http\Request),
> Object(Closure))
> #47 C:\wamp\www\clinic\vendor\laravel\framework\src\Illuminate\Pipeline\Pipeline.php(136):
> call_user_func_array(Array, Array)
> #48 [internal function]: Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}(Object(Illuminate\Http\Request))
> #49 C:\wamp\www\clinic\vendor\laravel\framework\src\Illuminate\Routing\Pipeline.php(32):
> call_user_func(Object(Closure), Object(Illuminate\Http\Request))
> #50 C:\wamp\www\clinic\vendor\laravel\framework\src\Illuminate\View\Middleware\ShareErrorsFromSession.php(49):
> Illuminate\Routing\Pipeline->Illuminate\Routing\{closure}(Object(Illuminate\Http\Request))
> #51 [internal function]: Illuminate\View\Middleware\ShareErrorsFromSession->handle(Object(Illuminate\Http\Request),
> Object(Closure))
> #52 C:\wamp\www\clinic\vendor\laravel\framework\src\Illuminate\Pipeline\Pipeline.php(136):
> call_user_func_array(Array, Array)
> #53 [internal function]: Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}(Object(Illuminate\Http\Request))
> #54 C:\wamp\www\clinic\vendor\laravel\framework\src\Illuminate\Routing\Pipeline.php(32):
> call_user_func(Object(Closure), Object(Illuminate\Http\Request))
> #55 C:\wamp\www\clinic\vendor\laravel\framework\src\Illuminate\Session\Middleware\StartSession.php(62):
> Illuminate\Routing\Pipeline->Illuminate\Routing\{closure}(Object(Illuminate\Http\Request))
> #56 [internal function]: Illuminate\Session\Middleware\StartSession->handle(Object(Illuminate\Http\Request),
> Object(Closure))
> #57 C:\wamp\www\clinic\vendor\laravel\framework\src\Illuminate\Pipeline\Pipeline.php(136):
> call_user_func_array(Array, Array)
> #58 [internal function]: Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}(Object(Illuminate\Http\Request))
> #59 C:\wamp\www\clinic\vendor\laravel\framework\src\Illuminate\Routing\Pipeline.php(32):
> call_user_func(Object(Closure), Object(Illuminate\Http\Request))
> #60 C:\wamp\www\clinic\vendor\laravel\framework\src\Illuminate\Cookie\Middleware\AddQueuedCookiesToResponse.php(37):
> Illuminate\Routing\Pipeline->Illuminate\Routing\{closure}(Object(Illuminate\Http\Request))
> #61 [internal function]: Illuminate\Cookie\Middleware\AddQueuedCookiesToResponse->handle(Object(Illuminate\Http\Request),
> Object(Closure))
> #62 C:\wamp\www\clinic\vendor\laravel\framework\src\Illuminate\Pipeline\Pipeline.php(136):
> call_user_func_array(Array, Array)
> #63 [internal function]: Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}(Object(Illuminate\Http\Request))
> #64 C:\wamp\www\clinic\vendor\laravel\framework\src\Illuminate\Routing\Pipeline.php(32):
> call_user_func(Object(Closure), Object(Illuminate\Http\Request))
> #65 C:\wamp\www\clinic\vendor\laravel\framework\src\Illuminate\Cookie\Middleware\EncryptCookies.php(59):
> Illuminate\Routing\Pipeline->Illuminate\Routing\{closure}(Object(Illuminate\Http\Request))
> #66 [internal function]: Illuminate\Cookie\Middleware\EncryptCookies->handle(Object(Illuminate\Http\Request),
> Object(Closure))
> #67 C:\wamp\www\clinic\vendor\laravel\framework\src\Illuminate\Pipeline\Pipeline.php(136):
> call_user_func_array(Array, Array)
> #68 [internal function]: Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}(Object(Illuminate\Http\Request))
> #69 C:\wamp\www\clinic\vendor\laravel\framework\src\Illuminate\Routing\Pipeline.php(32):
> call_user_func(Object(Closure), Object(Illuminate\Http\Request))
> #70 [internal function]: Illuminate\Routing\Pipeline->Illuminate\Routing\{closure}(Object(Illuminate\Http\Request))
> #71 C:\wamp\www\clinic\vendor\laravel\framework\src\Illuminate\Pipeline\Pipeline.php(103):
> call_user_func(Object(Closure), Object(Illuminate\Http\Request))
> #72 C:\wamp\www\clinic\vendor\laravel\framework\src\Illuminate\Routing\Router.php(726):
> Illuminate\Pipeline\Pipeline->then(Object(Closure))
> #73 C:\wamp\www\clinic\vendor\laravel\framework\src\Illuminate\Routing\Router.php(699):
> Illuminate\Routing\Router->runRouteWithinStack(Object(Illuminate\Routing\Route),
> Object(Illuminate\Http\Request))
> #74 C:\wamp\www\clinic\vendor\laravel\framework\src\Illuminate\Routing\Router.php(675):
> Illuminate\Routing\Router->dispatchToRoute(Object(Illuminate\Http\Request))
> #75 C:\wamp\www\clinic\vendor\laravel\framework\src\Illuminate\Foundation\Http\Kernel.php(246):
> Illuminate\Routing\Router->dispatch(Object(Illuminate\Http\Request))
> #76 [internal function]: Illuminate\Foundation\Http\Kernel->Illuminate\Foundation\Http\{closure}(Object(Illuminate\Http\Request))
> #77 C:\wamp\www\clinic\vendor\laravel\framework\src\Illuminate\Routing\Pipeline.php(52):
> call_user_func(Object(Closure), Object(Illuminate\Http\Request))
> #78 C:\wamp\www\clinic\vendor\laravel\framework\src\Illuminate\Foundation\Http\Middleware\CheckForMaintenanceMode.php(44):
> Illuminate\Routing\Pipeline->Illuminate\Routing\{closure}(Object(Illuminate\Http\Request))
> #79 [internal function]: Illuminate\Foundation\Http\Middleware\CheckForMaintenanceMode->handle(Object(Illuminate\Http\Request),
> Object(Closure))
> #80 C:\wamp\www\clinic\vendor\laravel\framework\src\Illuminate\Pipeline\Pipeline.php(136):
> call_user_func_array(Array, Array)
> #81 [internal function]: Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}(Object(Illuminate\Http\Request))
> #82 C:\wamp\www\clinic\vendor\laravel\framework\src\Illuminate\Routing\Pipeline.php(32):
> call_user_func(Object(Closure), Object(Illuminate\Http\Request))
> #83 [internal function]: Illuminate\Routing\Pipeline->Illuminate\Routing\{closure}(Object(Illuminate\Http\Request))
> #84 C:\wamp\www\clinic\vendor\laravel\framework\src\Illuminate\Pipeline\Pipeline.php(103):
> call_user_func(Object(Closure), Object(Illuminate\Http\Request))
> #85 C:\wamp\www\clinic\vendor\laravel\framework\src\Illuminate\Foundation\Http\Kernel.php(132):
> Illuminate\Pipeline\Pipeline->then(Object(Closure))
> #86 C:\wamp\www\clinic\vendor\laravel\framework\src\Illuminate\Foundation\Http\Kernel.php(99):
> Illuminate\Foundation\Http\Kernel->sendRequestThroughRouter(Object(Illuminate\Http\Request))
> #87 C:\wamp\www\clinic\public\index.php(54): Illuminate\Foundation\Http\Kernel->handle(Object(Illuminate\Http\Request))
> #88 {main}
最佳答案 你在Therapist Models中定义了__construct()方法,不是吗?
我猜你错过了$attributes param:
public function __construct($attributes = array())
{
parent::__construct($attributes);
//do your construction stuff
}