Laravel - 在 null 上调用成员函数 create()

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/38211024/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 14:05:57  来源:igfitidea点击:

Laravel - Call to a member function create() on null

phplaravelcontrollerlaravel-5.2

提问by Alex Harris

I am trying to save form details into my database, through a controller. When I click submit, I am shown this error:

我正在尝试通过控制器将表单详细信息保存到我的数据库中。当我单击提交时,显示此错误:

in ClinicController.php line 58 at ClinicController->store(object(Request))

在 ClinicController->store(object(Request)) 处的 ClinicController.php 第 58 行

Line 58 (See below for the entire function) is:

第 58 行(整个函数见下文)是:

$request->user()->create([

Controller:

控制器:

public function store(Request $request)
{

    $request->user()->create([
        'name' => $request->name,
        'email' => $request->email,
        'password' => $request->password
    ]);

    $request->user()->clinic()->create([
        'clinic_name' => $request->clinic_name,
        'telephone' => $request->telephone,
        'address_1' => $request->address_1,
        'address_2' => $request->address_2,
        'city' => $request->city,
        'postcode' => $request->postcode
    ]);

    return redirect('/home');

}

User Model:

用户模型:

public function clinic() {
    return $this->belongsTo(Clinic::class);
}

Clinic Model:

诊所模式:

public function user() {
    return $this->belongsTo(User::class);
}

Routes.php:

路线.php:

Route::post('/clinic', 'ClinicController@store');

Any help would be hugely appreciated. Many thanks.

任何帮助将不胜感激。非常感谢。

采纳答案by Ohgodwhy

If you want the currently authorized user, just use app()->user(). Also, you can simplify a lot of your logic and make it much easier to read.

如果您想要当前授权的用户,只需使用app()->user(). 此外,您可以简化很多逻辑并使其更易于阅读。

auth()
->user()
->create($request->only([
    'name', 
    'email', 
    'password'
]))
->clinic()
->create($request->only([
    'clinic_name', 
    'telephone', 
    'address_1', 
    'address_2', 
    'city', 
    'postcode'
]));

return redirect('/home');

回答by Alex Harris

public function store(Request $request)
{
    $user = App\User::create([
        'name' => $request->name,
        'email' => $request->email,
        'password' => $request->password
    ]);

    $clinic = App\Clinic::create([
        'clinic_name' => $request->clinic_name,
        'telephone' => $request->telephone,
        'address_1' => $request->address_1,
        'address_2' => $request->address_2,
        'city' => $request->city,
        'postcode' => $request->postcode
    ])

    $user->clinics()->attach($clinic->id);

    return redirect('/home');
}

回答by Panagiotis Koursaris

Change your controller to this:

将您的控制器更改为:

public function store(Request $request)
{

    App\User::create([
        'name' => $request->name,
        'email' => $request->email,
        'password' => $request->password
    ]);

    App\Clinic::create([
        'clinic_name' => $request->clinic_name,
        'telephone' => $request->telephone,
        'address_1' => $request->address_1,
        'address_2' => $request->address_2,
        'city' => $request->city,
        'postcode' => $request->postcode
    ]);

    return redirect('/home');

}

回答by Paul Androschuk

I think problems with $fillable property, make sure to check it.

我认为 $fillable 属性有问题,请务必检查它。