3
votes

How to check if user exists in my table or redirect back with a message using Laravel 5.6?

Type error: Argument 1 passed to Illuminate\Auth\SessionGuard::login() must be an instance of Illuminate\Contracts\Auth\Authenticatable, instance of Illuminate\Http\RedirectResponse given, called in C:\wamp\www\zainsurgalt\vendor\laravel\framework\src\Illuminate\Foundation\Auth\RegistersUsers.php on line 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']);
    }
}

I Added my Create method here

<?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']),
    ]);

}

}

5
You want to create a new user if they exist in the nigem table?Chukwuemeka Inya
Also, show us where you are trying to call this create method.Chukwuemeka Inya
@ChukwuemekaInya, in RegisterControllerRuka Xing
What's the condition... If the exist or if they don't exist? and what's the name of the table you are checking against?Chukwuemeka Inya
Niigem table when registering user's mail exists in Niigem table, Now can be registered If don't exists return message.Ruka Xing

5 Answers

2
votes
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']),
    ]);
}
3
votes

You need to return something when the user is created.

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']);
    }
}
2
votes

I see your are trying to create a new user if they exist in the niigem table. To do it the Laravel way, you have to validate using Laravel's validation class. So, this should work:

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
votes

Another way is:

Niigem::firstOrCreate(['email' => 'dummy@domain.com'], ['name'=> $data['name'], 'password' => bcrypt($data['password'])]);
0
votes

Please use this query you can use both condition as per your need:

$user = Niigem::where('email', '=', Input::get('email'))->first();

if ($user === null) {

// user doesn't exist

}

if ($user !== null)

{

// user doesn't exist

}