简体   繁体   English

如何解决试图在 Laravel 中获取非对象的属性

[英]How to solve Trying to get property of non-object in Laravel

I'm a beginner web developer I'm trying to write a condition that will be fulfilled depending on what data the user will send Condition if fulfilled by one hundred percent But the condition else produces an error with the following text我是初学者 web 开发人员 我正在尝试编写一个条件,该条件将根据用户将发送的数据而满足 条件如果满足 100% 但其他条件会产生错误,并显示以下文本

Trying to get property 'unique_id' of non-object试图获取非对象的属性“unique_id”

If someone have info how to solv this problem I will be grateful如果有人知道如何解决这个问题,我将不胜感激

This is my code from controller这是我来自 controller 的代码

public function checkRestorePassword(Request $request)
{
    $result['success'] = false;

    $rules = [
        'unique_id' => 'required',
    ];
    $validation = Validator::make($request->all(), $rules);
    if (empty($validation))
    {
        $result['error'] = "Enter the code that was sent to you by mail.";
        return response()->json($result, 422);
    }
    $user = User::where('unique_id', $request->uniqueId)->first();
    if ($user->unique_id === $request->uniqueId)
    {
        $result['success'] = 'Access to your personal account successfully restored. Please change your password.';
        return response()->json($result, 200);
    }
    else
    {
        $result['success'] = false;
        return response()->json($result, 422);
    }
}

I think it's because $user is returning null .我认为这是因为$user正在返回null

so do this所以这样做

if (!is_null($user) && $user->unique_id === $request->uniqueId)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM