0

我在存储用户数据时遇到问题,当用户注册时,我将一半信息存储在表用户中,另一半存储在另一个表中,当点击注册按钮时,我收到该错误,我发现表中用户信息存储正确但在其他表中什么都没有保存。

错误

in HasAttributes.php line 818
at HandleExceptions->handleError(2, 'Illegal offset type', 'C:\\wamp\\www\\Menoon1\\vendor\\laravel\\framework\\src\\Illuminate\\Database\\Eloquent\\Concerns\\HasAttributes.php', 818, array()) in HasAttributes.php line 818
at Model->getCasts() in HasAttributes.php line 803
at Model->hasCast('email', array('date', 'datetime')) in HasAttributes.php line 832
at Model->isDateCastable('email') in HasAttributes.php line 565
at Model->isDateAttribute('email') in HasAttributes.php line 525
at Model->setAttribute('email', 'dde@hg.com') in Model.php line 223
at Model->fill(array('email' => 'dde@hg.com')) in Model.php line 145
at Model->__construct(array('email' => 'dde@hg.com')) in Model.php line 268
at Model->newInstance(array('email' => 'dde@hg.com')) in Builder.php line 1030
at Builder->newModelInstance(array('email' => 'dde@hg.com')) in Builder.php line 723
at Builder->create(array('email' => 'dde@hg.com')) in Model.php line 1332
at Model->__call('create', array(array('email' => 'dde@hg.com'))) in Model.php line 1344
at user_emails->create(array('email' => 'dde@hg.com')) in Model.php line 1344
at Model::__callStatic('create', array(array('email' => 'dde@hg.com'))) in RegisterController.php line 94
at user_emails::create(array('email' => 'dde@hg.com')) in RegisterController.php line 94
at RegisterController->create(array('_token' => 'FZqd7Sc3fQRzmaQ0ImXYebzltzkygJsbKpNcZhv4', 'username' => 'vbnm v bnmm', 'email' => 'dde@hg.com', 'password' => '1111111', 'password_confirmation' => '1111111', 'fname' => 'vbn', 'lname' => 'bnm', 'address' => 'bnm', 'membership' => 'Normal', 'pic' => object(UploadedFile))) in RegistersUsers.php line 33
at RegisterController->register(object(Request))
at call_user_func_array(array(object(RegisterController), 'register'), array(object(Request))) in Controller.php line 55
at Controller->callAction('register', array(object(Request))) in ControllerDispatcher.php line 44
at ControllerDispatcher->dispatch(object(Route), object(RegisterController), 'register') in Route.php line 204
at Route->runController() in Route.php line 160
at Route->run() in Router.php line 559
at Router->Illuminate\Routing\{closure}(object(Request)) in Pipeline.php line 30
at Pipeline->Illuminate\Routing\{closure}(object(Request)) in RedirectIfAuthenticated.php line 24
at RedirectIfAuthenticated->handle(object(Request), object(Closure)) in Pipeline.php line 148
at Pipeline->Illuminate\Pipeline\{closure}(object(Request)) in Pipeline.php line 53
at Pipeline->Illuminate\Routing\{closure}(object(Request)) in SubstituteBindings.php line 41
at SubstituteBindings->handle(object(Request), object(Closure)) in Pipeline.php line 148
at Pipeline->Illuminate\Pipeline\{closure}(object(Request)) in Pipeline.php line 53
at Pipeline->Illuminate\Routing\{closure}(object(Request)) in VerifyCsrfToken.php line 65
at VerifyCsrfToken->handle(object(Request), object(Closure)) in Pipeline.php line 148
at Pipeline->Illuminate\Pipeline\{closure}(object(Request)) in Pipeline.php line 53
at Pipeline->Illuminate\Routing\{closure}(object(Request)) in ShareErrorsFromSession.php line 49
at ShareErrorsFromSession->handle(object(Request), object(Closure)) in Pipeline.php line 148
at Pipeline->Illuminate\Pipeline\{closure}(object(Request)) in Pipeline.php line 53
at Pipeline->Illuminate\Routing\{closure}(object(Request)) in StartSession.php line 64
at StartSession->handle(object(Request), object(Closure)) in Pipeline.php line 148
at Pipeline->Illuminate\Pipeline\{closure}(object(Request)) in Pipeline.php line 53
at Pipeline->Illuminate\Routing\{closure}(object(Request)) in AddQueuedCookiesToResponse.php line 37
at AddQueuedCookiesToResponse->handle(object(Request), object(Closure)) in Pipeline.php line 148
at Pipeline->Illuminate\Pipeline\{closure}(object(Request)) in Pipeline.php line 53
at Pipeline->Illuminate\Routing\{closure}(object(Request)) in EncryptCookies.php line 59
at EncryptCookies->handle(object(Request), object(Closure)) in Pipeline.php line 148
at Pipeline->Illuminate\Pipeline\{closure}(object(Request)) in Pipeline.php line 53
at Pipeline->Illuminate\Routing\{closure}(object(Request)) in Pipeline.php line 102
at Pipeline->then(object(Closure)) in Router.php line 561
at Router->runRouteWithinStack(object(Route), object(Request)) in Router.php line 520
at Router->dispatchToRoute(object(Request)) in Router.php line 498
at Router->dispatch(object(Request)) in Kernel.php line 174
at Kernel->Illuminate\Foundation\Http\{closure}(object(Request)) in Pipeline.php line 30
at Pipeline->Illuminate\Routing\{closure}(object(Request)) in TransformsRequest.php line 30
at TransformsRequest->handle(object(Request), object(Closure)) in Pipeline.php line 148
at Pipeline->Illuminate\Pipeline\{closure}(object(Request)) in Pipeline.php line 53
at Pipeline->Illuminate\Routing\{closure}(object(Request)) in TransformsRequest.php line 30
at TransformsRequest->handle(object(Request), object(Closure)) in Pipeline.php line 148
at Pipeline->Illuminate\Pipeline\{closure}(object(Request)) in Pipeline.php line 53
at Pipeline->Illuminate\Routing\{closure}(object(Request)) in ValidatePostSize.php line 27
at ValidatePostSize->handle(object(Request), object(Closure)) in Pipeline.php line 148
at Pipeline->Illuminate\Pipeline\{closure}(object(Request)) in Pipeline.php line 53
at Pipeline->Illuminate\Routing\{closure}(object(Request)) in CheckForMaintenanceMode.php line 46
at CheckForMaintenanceMode->handle(object(Request), object(Closure)) in Pipeline.php line 148
at Pipeline->Illuminate\Pipeline\{closure}(object(Request)) in Pipeline.php line 53
at Pipeline->Illuminate\Routing\{closure}(object(Request)) in Pipeline.php line 102
at Pipeline->then(object(Closure)) in Kernel.php line 149
at Kernel->sendRequestThroughRouter(object(Request)) in Kernel.php line 116
at Kernel->handle(object(Request)) in index.php line 54
at require_once('C:\\wamp\\www\\Menoon1\\public\\index.php') in server.php line 21

用户模式

 public function useremail()
    {
        return $this->hasOne('App\user_emails');
    }

其他表代码

public function user()
    {
        return $this->belongsTo('App\User');
    }

寄存器控制器中的代码

  protected function create(array $data)
    {

      $fileName = 'null';
      Input::file('pic')->store('avatars');
      if (Input::file('pic')->isValid()) {
        $destinationPath = public_path('uploads/files');
        $extension = Input::file('pic')->getClientOriginalExtension();
        $fileName = uniqid().'.'.$extension;

        Input::file('pic')->move($destinationPath, $fileName);
    }
          

        $user= User::create([
            'fname' => $data['fname'],
            'lname' => $data['lname'],
            'address' => $data['address'],
            'username' => $data['username'],         
            'membership' => $data['membership'],
            'pic'=>$fileName,
            'password' => bcrypt($data['password']),
        ]);
        $useremail=user_emails::create(['email'=>$data['email']]);
       // dd($useremail);
        $user->useremail()->save($useremail);
        return $user;
    }

4

3 回答 3

3

我不知道你的模型,但如果有复合键,集合无法获取属性

于 2017-10-11T14:35:11.843 回答
0

尝试

$user->useremail()->save(new UserEmail(['email'=>$data['email']]));

假设 useremail 的模型是 UserEmail。

而且您的模型混淆了:它应该用于 App\UserEmail:

public function user()
{
    return $this->belongsTo('App\User');
}

对于应用\用户

public function userEmail()
{
    return $this->hasOne('App\user_emails');
}

为了您自己的利益,请注意约定。'useremail' 成为 userEmail'。模型文件变为 User.php 和 UserEmail.php。你的表'users'和'user_emails'分别。因此,“user_emails”中的关系应该是“user_id”。并做了。

于 2017-04-22T11:53:48.083 回答
0

我遇到了同样的问题,它发生是因为在模型中我还定义了一个主键,但是在创建表时它已经在数据库中设置了一个子句。

于 2021-09-14T21:30:17.073 回答