Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

AbdulBazith's avatar

Manual Authentication process with user defined table in laravel

Guys iam working with a project job portal site.

for job seeker i done manual authentication with user table

this is my controller for manual authentication process

public function store(Request $request)
    {
        if (auth()->attempt(request(['email', 'password'])) == false) {
            return back()->withErrors([
                'message' => 'The email or password is incorrect, please try again'
            ]);
        }

        return redirect()->route('login_logout.index');     

    }


this works fine. perfectly authenticating and logging the user.

this is my model for that user table

<?php

namespace App;

use Illuminate\Notifications\Notifiable;

use Illuminate\Foundation\Auth\User as Authenticatable;

class User extends Authenticatable
{

    use Notifiable;

    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = [
        'user_type','email',  'mobile_number',  'password','type','first_name','last_name','progress_value','is_accepted',
    ];

    /**
     * The attributes that should be hidden for arrays.
     *
     * @var array
     */
    protected $hidden = [
        'password', 'remember_token',
    ];

     /**
     * Add a mutator to ensure hashed passwords
     */

    public function setPasswordAttribute($password)
    {
        $this->attributes['password'] = bcrypt($password);
    }
}

Now i need to authenticate employer registration.

i created a model EmployerUser

this is my model

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

use Illuminate\Notifications\Notifiable;

class EmployerUser extends Model
{
    //

    use Notifiable;

    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = [
        'user_type','email_id',  'mobile',  'password','is_accepted',
    ];

    /**
     * The attributes that should be hidden for arrays.
     *
     * @var array
     */
    protected $hidden = [
        'password', 'remember_token',
    ];

     /**
     * Add a mutator to ensure hashed passwords
     */

    public function setPasswordAttribute($password)
    {
        $this->attributes['password'] = bcrypt($password);
    }
}



and i inserted data with the cotroller.

$employer_user =  EmployerUser::create([
                'email_id' => $request->email_id,
                'mobile' => $request->mobile,
                'password' =>$request->mobile,
                'user_type'=>'Employer',
                'is_accepted'=>0,

            ]);


this also worked fine.

but when it comes to authentication. there comes the problem

this is what i did to authenticate employer

public function employer_login(Request $request)
    {
        if (auth()->attempt(request(['email', 'password'])) == false) {
            return back()->withErrors([
                'message' => 'The email or password is incorrect, please try again'
            ]);
        }
        return redirect()->route('Employer-registration.index');

    }

But this is authenticating with user table..

what can i do

i placed employeruser() here if (auth()->employeruser()->attempt(request(['email', 'password'])) == false)

but showing error

Method employeruser does not exist.

how can i authenticate

and after login in my user i used this to fetch all records $user = Auth::user(); this fetched all records.

but if i give $employeruser = Auth::employeruser (); it showing the same error

Method employeruser does not exist.

What should i do??

Kindly some one help please..

0 likes
14 replies
munazzil's avatar

I think you missed a $ sign in request after the attempt.Just updated as like below.

  public function employer_login(Request $request)
 {
        if (auth()->attempt($request(['email', 'password'])) == false) {
          return back()->withErrors([
            'message' => 'The email or password is incorrect, please try again'
        ]);
     }
       return redirect()->route('Employer-registration.index');

 }
1 like
AbdulBazith's avatar

@snapey @munazzil thank you for your responses.

@munazzil actually thats not my problem. the model and table is the problem here.

the code which u gave is for user model authentication

but what i had used is EmployerUser model for authentication

1 like
munazzil's avatar

In your controller you have used $request->mobile instead of $request->password please check that,

$employer_user =  EmployerUser::create([
            'email_id' => $request->email_id,
            'mobile' => $request->mobile,
            'password' =>$request->password,
            'user_type'=>'Employer',
            'is_accepted'=>0,

            ]);
1 like
AbdulBazith's avatar

@munazzil

that worked fine. because at present i only kept mobile number as password.

simply to say how i can authenticate manually EmployerUser table

1 like
munazzil's avatar

In your HomeController use as like below.

     class HomeController extends Controller
 {
    public $user;

    public function __construct()
    {
        $this->middleware(function ($request, $next) {
                $this->$user = auth()->employeruser();

                return $next($request);
        });
    }
 }
1 like
AbdulBazith's avatar

@munazzil so that what hapens?

Where iam checking the condition?

if email and password is corect that condition?

1 like
munazzil's avatar

While check your condition before the return $next($request);

1 like
Snapey's avatar
Snapey
Best Answer
Level 122

He's just throwing out random stuff... no real idea of the problem.

You have made life difficult for yourself by having two tables. I always treat all users the same but have additional properties to differentiate them.

If you wish to persist with multiple auths then you are going to have to put some work in to resolve all the issues. Google for Laravel Multi-Auth for some ideas.

1 like
anthonyfielding's avatar

The SSID is actually the name of the wireless network, which by default is usually the name of the networking company, such as Netgear, Linksys, or D-Link. By turning off the broadcast, the name of the network will not show up on the Windows or https://www.manhattanmedicalarts.com/ Mac list of available networks

Please or to participate in this conversation.