【问题标题】:laravel 5.4 - comparing user input to db valuelaravel 5.4 - 将用户输入与数据库值进行比较
【发布时间】:2017-08-05 14:09:11
【问题描述】:

我使用了 laravel 的 make:auth 并且正在玩注册部分。我正在尝试将用户的输入与我的数据库中已有的值进行比较,如果存在,那么用户将继续注册,如果不存在,它将把用户带到另一个页面。现在,执行该功能后,它会直接将用户带到另一个页面。似乎是什么问题?

public function register(Request $request)
{
    $name = $request->name;
    $lastname = $request->lastname;
    $check = DB::table('records')->where([
        ['firstname','=',$name],
        ['lastname','=',$lastname]
        ])->get();
    if ($check===null) {
        $this->validator($request->all())->validate();

        event(new Registered($user = $this->create($request->all())));

        $this->guard()->login($user);

        return $this->registered($request, $user)
                        ?: redirect($this->redirectPath());
    } else {
        return redirect('/');
    }

}

【问题讨论】:

    标签: php database laravel laravel-5.4


    【解决方案1】:

    get()方法返回一个collection,所以如果没有记录$check会类似于

    $a = collect([]);
    

    $a === null // returns false
    $a->isEmpty(); // returns true
    count($a); // returns 0
    

    因此您可能希望在 if 子句中使用不同的比较,这将是

    如果存在,则用户将继续注册

    if (!$check->isEmpty()) {
        // proceed to registration
    } else {
        // redirect
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-05-24
      • 2021-12-27
      • 2016-06-15
      • 1970-01-01
      相关资源
      最近更新 更多