Laravel - 验证数据库电子邮件字段以进行验证

时间:2017-03-14 09:46:53

标签: php laravel

我正在开展一个小型的个人项目,我想询问是否有可能对“verified_employee”的数据库值进行auth检查。在我当前的设置中,“verified_employee”是一个布尔数据库字段,默认设置为0。

我的问题如下: “在刀片视图(即”Home.blade.php“)中是否可以运行”if auth :: user() - > verified_employee ='1'“之类的检查,然后让用户继续。如果没有,请调整这样的视图显示消息的意思是“您的帐户未被管理团队激活。请等待他们这样做“。

用户模型

<?php

namespace App;

use Illuminate\Notifications\Notifiable;
use Illuminate\Foundation\Auth\User as Authenticatable;
use App\Hour;

class User extends Authenticatable
{
use Notifiable;

/**
 * The attributes that are mass assignable.
 *
 * @var array
 */
protected $fillable = [
    'name', 'email', 'password', 'admin', 'verified_employee'
];

public function Hours() {
    return $this->hasMany(Hour::class);
}

/**
 * The attributes that should be hidden for arrays.
 *
 * @var array
 */
protected $hidden = [
    'password', 'remember_token',
];
}

查看

@extends('layouts.app')

@section('content')
<div class="container">
<div class="row">
    <div class="col-md-8 col-md-offset-2">
        <div class="panel panel-default">
            <div class="panel-heading">Dashboard</div>
            <!-- CHECK IF user->verfied_employee is true -->
            <!-- IF YES -->
            <div class="panel-body">
                Welkom, {{ Auth::user()->name }}
            </div>
            <!-- IF NO -->
            <div class="panel-body">
                Please wait till your account has been verified by the team.
            </div>
        </div>
    </div>
</div>
</div>
@endsection

验证 - &gt;登录控制器

<?php

namespace App\Http\Controllers\Auth;

use App\Http\Controllers\Controller;
use Illuminate\Foundation\Auth\AuthenticatesUsers;

class LoginController extends Controller
{
/*
|--------------------------------------------------------------------------
| Login Controller
|--------------------------------------------------------------------------
|
| This controller handles authenticating users for the application and
| redirecting them to your home screen. The controller uses a trait
| to conveniently provide its functionality to your applications.
|
*/

use AuthenticatesUsers;

/**
 * Where to redirect users after login.
 *
 * @var string
 */
protected $redirectTo = '/home';

/**
 * Create a new controller instance.
 *
 * @return void
 */
public function __construct()
{
    $this->middleware('guest', ['except' => 'logout']);
}
}

3 个答案:

答案 0 :(得分:1)

是的,完全没问题

@if( Auth::check() && Auth::user()->verified_employee )
    //do stuff
@endif

您的字段是布尔值,因此它将返回true或false。

答案 1 :(得分:0)

如果您不想显示视图,可以使用控制器方法执行此操作:

if (auth()->check() && auth()->user()->verified_employee) {
    return view(....);
} else {
    return redirect('/')->with('message', $message);
}

答案 2 :(得分:0)

您可以在刀片中使用ifs,如下所示:

@if(Auth::user()->verified_employee)

<some-html></some-html>

@else

<other-html></other-html>

@endif

假设verified_employee是一个布尔值,那应该可以做到。