嘿我正在制作一个项目,其中有多个用户,不同的人将要登录。用户根据其角色将显示不同的页面。我已经制作了一段代码但是每当用户登录时他/她都可以访问另一个用户的页面。
这是我的登录控制器
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\User;
use Auth;
class LoginController extends Controller
{
public function login(Request $request)
{
if ( Auth::attempt(request(['email', 'password'])) )
{
$user = User::where('email', $request->email)->firstOrFail();
if($user->is_admin())
{
return redirect()->route('dashboard');
}
return redirect()->route('home');
}
redirect()->back();
}
}
这些是我的路线
<?php
Auth::routes();
Route::get('/home', 'HomeController@index')->name('home');
Route::group(['middleware' => 'auth'], function() {
Route::post('/login/custom', ['uses' => 'LoginController@login', 'as' => 'login.custom']);
Route::get('/login/custom', ['uses' => 'LoginController@login', 'as' => 'login.custom']);
Route::get('/home', function() { return view('home'); })->name('home');
Route::get('/dashboard',function(){ return view('dashboard'); })->name('dashboard');
});
这是我的模特
<?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 = [
'name',
'email',
'password',
];
/**
* The attributes that should be hidden for arrays.
*
* @var array
*/
protected $hidden = [
'password',
'remember_token',
];
public function is_admin(){
if(!$this->admin)
return false;
return true;
}
}
请告诉我我做错了什么。
答案 0 :(得分:0)
没有动机使用is_admin()
功能而非使用->admin
(仅在迁移时设置为Boolean
/ Tinyint
)。
如果true
为->admin
,则返回true
,反之亦然......
也许您的is_admin
功能无法正常运行,如果有人是管理员,它总是返回相同的值事件。
还可以在问题中包含您的create_users_table
迁移代码。