Auth :: login ($ user) laravel

0

I'm using Auth::login($user); to log in manually
using fluent / querybuilder

  

Example

    $count = DB::table('users')
             ->where('username', '=', $username)
             ->where('password', '=', $password)
             ->count();


    if($count > 0 ){
         $user = DB::table('users')
                 ->where('login', '=', $username)
                 ->where('password', '=', $password)
                 ->get();

         Auth::login($user);
         return redirect("/");

   }else{
      return "datos incorrectos";
   }

what laravel says:

  

Type error: Argument 1 passed to Illuminate \ Auth \ Guard :: login () must   be an instance of Illuminate \ Contracts \ Auth \ Authenticatable, array   given

Is there any way to make it work?

    
asked by Devil System 06.09.2016 в 20:58
source

1 answer

1

If you want to use the Laravel authentication system you must use its interfaces, in fact it is not generally a good practice to use the DB facade unless it is strictly necessary.

In order to work, you must pass an object that is an instance of Authenticatable, as the definition of the method shows:

public function login(AuthenticatableContract $user, $remember = false)

In that order of ideas the best thing would be to use Eloquent, something like this in the model:
use Illuminate\Contracts\Auth\Authenticatable as AuthenticatableContract;

class User implements AuthenticatableContract
{
    protected table = 'users';
}

Login

$user = User::where('login', $username)->where('password', $password)->first();

If you definitely do not want to use Eloquent, then you extend the class Illuminate\Auth\SessionGuard , more specifically the login method via a ServiceProvider.

If it does not work yet then you should create your own authentication system, replacing the current ServiceProvider, starting with config / app.php and config / auth.php

    
answered by 06.09.2016 в 22:00