【问题标题】:Laravel - Auth check on database email field for verificationLaravel - 对数据库电子邮件字段进行身份验证以进行验证
【发布时间】:2017-03-14 17:51:21
【问题描述】:

我正在从事一个小型个人项目,我想问一下是否有可能对“verified_employee”的数据库值运行身份验证检查。在我当前的设置中,“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

Auth -> 登录控制器

<?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']);
}
}

【问题讨论】:

    标签: php laravel


    【解决方案1】:

    是的,没问题

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

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

    【讨论】:

      【解决方案2】:

      你可以在刀片中使用 ifs,像这样:

      @if(Auth::user()->verified_employee)
      
      <some-html></some-html>
      
      @else
      
      <other-html></other-html>
      
      @endif
      

      假设 verified_employee 是一个布尔值,这应该可以解决问题。

      【讨论】:

      • 检查它是 1 还是 0 的方法是什么? (考虑到刀片结构看起来我们只是在访问 verify_employee 字段)
      • @if(Auth::user()->verified_employee == 1) 检查 1,只是常规的 if 语法
      【解决方案3】:

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

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

      【讨论】:

        猜你喜欢
        • 2018-04-01
        • 1970-01-01
        • 1970-01-01
        • 2016-12-13
        • 2013-07-01
        • 1970-01-01
        • 1970-01-01
        • 2012-02-03
        • 1970-01-01
        相关资源
        最近更新 更多