I have a login that worked perfect with the id in its respective table, change the migration so that the primary one was username (in table users), deleting the id, I put in the model the new primaryKey "username", but the auth::attempt
always returns false and the data is going well in the form.
User Model
<?php
namespace App;
use Illuminate\Notifications\Notifiable;
use Illuminate\Foundation\Auth\User as Authenticatable;
class User extends Authenticatable
{
use Notifiable;
protected $primaryKey = 'username';
public $incrementing = false;
// protected $primary_key = 'username';
//public $incrementing = false;
/**
* The attributes that are mass assignable.
*
* @var array
*/
protected $fillable = [
'username', 'password', 'tipo_usuario',
];
/**
* The attributes that should be hidden for arrays.
*
* @var array
*/
protected $hidden = [
'password', 'remember_token',
];
}
LoginController
<?php
namespace App\Http\Controllers\Auth;
use Auth;
use App\Http\Controllers\Controller;
class LoginController extends Controller
{
public function __construct()
{
$this->middleware('guest', ['only' => 'showLoginForm']);
}
public function showLoginForm()
{
return view('auth.login');
}
public function login()
{
$credentials = $this->validate(request(), [
$this->username() => 'required|string|min:4',
$this->password() => 'required|string|min:6',
]);
if (Auth::attempt($credentials))
{
return $credentials;
//return redirect()->route('dashboard');
}
return back()
->withErrors([$this->username() => 'Estas credenciales no coinciden con nuestros registros'])
->withInput(request([$this->username()]));
}
public function logout()
{
Auth::logout();
return redirect('');
}
public function username()
{
return 'username';
}
public function password()
{
return 'password';
}
}