如何使用Laravel 5.6检查用户是否存在于我的表中或重定向回一条消息?
类型错误:传递给Illuminate \ Auth \ SessionGuard :: login()的参数1必须是Illuminate \ Contracts \ Auth \ Authenticatable的实例,给出的Illuminate \ Http \ RedirectResponse实例,在C:\ wamp \ www中调用\ zainsurgalt \ vendor \ laravel \ framework \ src \ Illuminate \ Foundation \ Auth \ RegistersUsers.php,第35行
protected function create(array $data)
{
$isEmailExists = Niigem::where('email', $data['email'])->count();
if($isEmailExists){
User::create([
'name' => $data['name'],
'email' => $data['email'],
'password' => bcrypt($data['password']),
]);
}
else{
return Redirect::back()->withErrors(['msg', 'The Message']);
}
}
我在这里添加了我的Create方法
<?php
namespace App\Http\Controllers\Auth;
use App\User;
use App\Niigem;
use Validator;
use Illuminate\Support\Facades\Redirect;
use App\Http\Controllers\Controller;
use Illuminate\Foundation\Auth\RegistersUsers;
class RegisterController extends Controller
{
use RegistersUsers;
protected $redirectTo = '/home';
public function __construct()
{
$this->middleware('guest');
}
protected function validator(array $data)
{
return Validator::make($data, [
'name' => 'required|max:255',
'email' => 'required|max:255|unique:users',
'password' => 'required|min:6|confirmed',
]);
}
protected function create(array $data){
$validator = Validator::make($data, [
'name' => 'required|string',
'email' => 'required|string|email|exists:niigems',
'password' => 'required|string',
]);
if ($validator->fails()) {
return Redirect::back()
->withErrors($validator)
->withInput();
}
return User::create([
'name' => $data['name'],
'email' => $data['email'],
'password' => Hash::make($data['password']),
]);
}
}
答案 0 :(得分:3)
创建用户时,您需要返回一些内容。
protected function create(array $data)
{
$isEmailExists = Niigem::where('email', $data['email'])->count();
if($isEmailExists){
User::create([
'name' => $data['name'],
'email' => $data['email'],
'password' => bcrypt($data['password']),
]);
return ........ you need to return something here
} else {
return Redirect::back()->withErrors(['msg', 'The Message']);
}
}
答案 1 :(得分:2)
如果您在niigem
表中存在一个新用户,我看到您正在尝试创建一个新用户。为此,您必须使用Laravel的validation
类进行验证。因此,这应该可行:
protected function create(array $data)
{
$validator = Validator::make($data, [
'name' => 'required|string',
'email' => 'required|string|email|exists:niigem',
'password' => 'required|string',
]);
if ($validator->fails()) {
return Redirect::back()
->withErrors($validator)
->withInput();
}
return User::create([
'name' => $data['name'],
'email' => $data['email'],
'password' => Hash::make($data['password']),
]);
}
答案 2 :(得分:2)
另一种方法是:
Niigem::firstOrCreate(['email' => 'dummy@domain.com'], ['name'=> $data['name'], 'password' => bcrypt($data['password'])]);
答案 3 :(得分:1)
protected function validator(array $data)
{
return Validator::make($data, [
'name' => 'required|max:255',
'email' => 'required|max:255|unique:users|exists:niigems',
'password' => 'required|min:6|confirmed',
]);
}
protected function create(array $data)
{
User::create([
'name' => $data['name'],
'email' => $data['email'],
'password' => bcrypt($data['password']),
]);
}
答案 4 :(得分:0)
请使用此查询,您可以根据需要使用两种条件:
$ user = Niigem :: where('email','=',Input :: get('email'))-> first();
if($ user === null){
//用户不存在
}
如果($ user!== null)
{
//用户不存在
}