First i created the Admin model in which i implemented it using Authenticable Admin.php
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Auth\Authenticatable;
class Admin extends Model implements \Illuminate\Contracts\Auth\Authenticatable
{
//
use Authenticatable;
}
then i created the migration :
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreateAdminsTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('admins', function (Blueprint $table) {
$table->increments('id');
$table->timestamps();
$table->rememberToken();
$table->string('email');
$table->string('password');
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::drop('admins');
}
}
Replaced the Users model in config/auth.php with Admin Model :
'providers' => [
'users' => [
'driver' => 'eloquent',
'model' => App\Admin::class,
],
AdminController.php
<?php
namespace App\Http\Controllers;
use App\Post;
use Illuminate\Http\Request;
use Auth;
class AdminController extends Controller{
public function getIndex()
{
$posts= Post::orderBy('created_at','desc')->take(3)->get();
return view('backend.index')->with(['posts'=>$posts]);
}
public function getLogin(){
return view('backend.login');
}
public function postLogin(Request $request){
$this->validate($request,[
'email'=>'required|email',
'password'=>'required'
]);
dd(Auth::attempt(['email'=>$request['email'],'password'=>$request['password']]));
if(!Auth::attempt(['email'=>$request['email'],'password'=>$request['password']])){
return redirect()->back()->with(['fail'=>'Could Not Log You In']);
}
return redirect()->route('admin.index');
}
public function getLogout(){
Auth::logout();
return redirect()->route('blog.index');
}
}
web.php
Route::get('/admin/login',[
'uses'=>'AdminController@getLogin',
'as'=>'admin.login'
]);
Route::post('/admin/login',[
'uses'=>'AdminController@postLogin',
'as'=>'admin.login'
]);
login.blade.php
<!DOCTYPE html>
<html lang="en">
<head>
<title>Admin Area</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="{{URL::to('css/bootstrap.min.css')}}">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script src="{{URL::to('js/bootstrap.min.js')}}"></script>
@yield('styles')
</head>
<body>
<div class="container">
<div class="container-fluid">
@include('includes.info-box')
<form method="post" action="{{route('admin.login')}}">
<div class="form-group">
<label for="email">Email: </label>
<input type="email" class="form-control" id="email" name="email"
{{$errors->has('email')?'class=alert alert-danger':'' }}
value="{{Request::old('email')}}">
</div>
<div class="form-group">
<label for="password">Password: </label>
<input type="password" class="form-control" id="password" name="password"
{{$errors->has('password')?'class=alert alert-danger':'' }}
>
</div>
<div class="form-group">
<button type="submit" class="btn btn-default">Login</button>
<input type="hidden" name="_token" value="{{Session::token()}}">
</div>
</form>
</div>
</div>
</body>
</html>
so when i tried to login and dd the Auth Attempt it is returning false i have a user with email [email protected] and password test :
created the user using seeder :
<?php
use Illuminate\Database\Seeder;
class AdminTableSeeder extends Seeder
{
/**
* Run the database seeds.
*
* @return void
*/
public function run()
{
//
$admin = new \App\Admin;
$admin->email='[email protected]';
$admin->password='[email protected]';
$admin->save();
}
}