我的代码是:
$user = User::create([
'email' => $request->email,
'password' => Hash::make($request->password),
]);
$user_details = [
'name' => $request->name,
'address' => $request->address,
'lastname' => $request->lastname,
'secondname' => $request->secondname,
'inn' => $request->inn,
'fincode' => $request->fincode,
];
$user->details()->create($user_details);
模型用户是:
class User extends Authenticatable implements JWTSubject
{
use Notifiable;
public function details()
{
return $this->hasOne(UserDetails::class, 'user_id', 'id');
}
}
UserDetails 模型是:
namespace App;
use Illuminate\Database\Eloquent\Model;
class UserDetails extends Model
{
protected $table = 'enterprise';
protected $fillable = ['name', 'lastname', 'secondname', 'address', 'inn', 'fincode'];
}
答案 0 :(得分:1)
我相信您的$user
尚未保留,因此出现错误,在您的情况下,$user
为null,这就是为什么您无法在details
对象上调用null
的原因。确保您在用户上使用了所有必填字段。
如果共享的内容是完整内容,则fillable
模型中的User
数组可能会丢失,然后添加以下内容:
protected $fillable = [ 'email', 'password'];