Ruka Xing
Ruka Xing

Reputation: 642

Laravel 5 how to check User exists in my other table

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

}

}

Upvotes: 3

Views: 4255

Answers (5)

Elliot
Elliot

Reputation: 580

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

Upvotes: 2

ujjwal
ujjwal

Reputation: 34

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

}

Upvotes: 0

Rockwood
Rockwood

Reputation: 21

Another way is:

Niigem::firstOrCreate(['email' => '[email protected]'], ['name'=> $data['name'], 'password' => bcrypt($data['password'])]);

Upvotes: 2

Chukwuemeka Inya
Chukwuemeka Inya

Reputation: 2671

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

Upvotes: 2

zjbarg
zjbarg

Reputation: 679

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

Upvotes: 3

Related Questions