如何解决登录后如何在刀片页面 Laravel 5.8 中显示用户详细信息
我想要如果 type_user = 1 并使用 Auth:check id 进行验证,然后打印 VTPL Administration,然后 type_user = 2 & 3
我将如何在刀片页面中查看它。
这里是刀片页面
这是我的用户模型
控制器代码
请帮帮我....
解决方法
@dump(Auth::user()->type_user);
@if (Auth::check())
<div class="info">
<a class="d-block">{{ Auth::user()->name ?? ""}}</a>
</div>
@endif
,
@if (Auth::check())
<p> {{Auth::User()->type_user == 1 ? Auth::User()->name : ""}} </p>
@endif
只是,根据条件使用三元运算符。
根据您在控制器代码中的字段进行更改
public function authCheck() {
$input = Request::all();
$rule = array(
'email' => 'required|exists:users','password' => 'required'
);
$validation = Validator::make($input,$rule,array());
if ($validation->fails()) {
$messages = $validation->errors();
return Redirect::back()->withInput()->withErrors($messages);
}
$cred = [
'email' => $input['email'],'password' => $input['password'],];
Auth::attempt($cred,true);
if (Auth::check()) {
$user_type = Auth::User()->type_user;
if ($user_type == 1) {
return redirect()->to('/adminlogin');
} else {
return redirect()->to('/dashboard');
}
} else {
$message = 'Username or password is incorrect';
return Redirect::back()->withInput()->with('error',$message);
}
}
模型的变化
namespace App;
use Illuminate\Notifications\Notifiable;
use Illuminate\Foundation\Auth\User as Authenticatable;
class AdminUserLogin extends Authenticatable {
use Notifiable;
protected $table = 'user';
protected $fillable = [
'name','email','password','status','type_user',];
protected $casts = [
'type_user' => 'integer','status' => 'integer',];
protected $hidden = [
'password','remember_token','created_at','updated_at'
];}
希望它能轻松帮到你。
,您可以在 type_user_name
模型中定义附加字段 getTypeUserName
和 App/Models/User
Accessor。
class User extends Authenticatable
{
use Notifiable;
protected $appends = ['type_user_name'];
public function getTypeUserNameAttribute()
{
$type_user = $this->attributes['type_user'];
if ($type_user == 1) return "VTPL Administration";
if ($type_user == 2) return "User 2";
if ($type_user == 3) return "User 3";
}
}
因此,无论何时查询 auth()->user()
,都会填充 type_user_name
字段。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。