<?php
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreateUsersTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('users', function (Blueprint $table) {
$table->increments('id');
$table->string('name');
$table->string('email')->unique();
$table->string('password');
$table->rememberToken();
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::drop('users');
}
}
database/seeds/DatabaseSeeder.php
<?php
use Illuminate\Database\Seeder;
class DatabaseSeeder extends Seeder
{
/**
* Run the database seeds.
*
* @return void
*/
public function run()
{
$this->call(UsersTableSeeder::class);
}
}
database/seeds/UsersTableSeeder.php
<?php
use Illuminate\Database\Seeder;
class UsersTableSeeder extends Seeder
{
/**
* Run the database seeds.
*
* @return void
*/
public function run()
{
$array = [
['name' => 'admin', 'email' => '[email protected]', 'password' => 'admin'],
];
// Loop through each user above and create the record for them in the database
foreach ($array as $user) {
$obj = new App\User($user);
$obj->save();
}
}
}
app/User.php
<?php
namespace App;
use Illuminate\Foundation\Auth\User as Authenticatable;
class User extends Authenticatable
{
/**
* The attributes that are mass assignable.
*
* @var array
*/
protected $fillable = [
'name', 'email', 'password',
];
/**
* The attributes that should be hidden for arrays.
*
* @var array
*/
protected $hidden = [
'password', 'remember_token',
];
}
app/Http/routes.php
// Auth Route...
Route::controller('/auth', 'AuthController');
// Member Route...
Route::group(['middleware' => 'auth'], function () {
Route::get('/', function () {
return view('welcome');
});
});
app/Http/Controllers/Auth/AuthController.php
<?php
namespace App\Http\Controllers\Auth;
use App\User;
use App\Http\Controllers\Controller;
class AuthController extends Controller
{
/**
* Where to redirect users after login / registration.
*
* @var string
*/
protected $redirectTo = '/';
/**
* Create a new authentication controller instance.
*
* @return void
*/
public function __construct()
{
$this->middleware($this->guestMiddleware(), ['except' => 'logout']);
}
public function getLogin() {
return view('webapp.login');
}
public function postLogin(Request $request) {
// grab credentials from the request
$credentials = $request->only('email', 'password');
$rules = [
'email' => 'required|exists:users,email,deleted_at,NULL',
'password' => 'required|min:6',
];
$attributeNames = [
'email' => 'บัญชีผู้ใช้',
'password' => 'รหัสผ่าน',
];
$validator = Validator::make($credentials, $rules);
$validator->setAttributeNames($attributeNames);
if ($validator->fails()) {
return redirect()->back()
->withErrors('ชื่อผู้ใช้หรือรหัสผ่านไม่ถูกต้อง')
->withInput($request->except('password'));
}
else {
if (Auth::attempt($credentials, $request->has('remember'))) {
return redirect()->route('index');
}
else {
return redirect()->back()
->withErrors('ชื่อผู้ใช้หรือรหัสผ่านไม่ถูกต้อง')
->withInput($request->except('password'));
}
}
}
public function getLogout() {
Auth::logout();
return redirect()->route('auth.login');
}
}