【问题标题】:Laravel middleware logic doesn't seem to workLaravel 中间件逻辑似乎不起作用
【发布时间】:2019-05-11 23:46:49
【问题描述】:

我正在尝试创建一个中间件,如果在标识表上未找到任何数据,该中间件会将用户重定向到验证页面,但我不断得到 if 语句的逻辑错误

我已经尝试将$request->user()->identification->()has('user_id) 作为我的 if 语句

//从我的标识中间件,我有这个;

<?php

namespace App\Http\Middleware;


use Closure;

class Identification
{
    /**
     * Handle an incoming request.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Closure  $next
     * @return mixed
     */
    public function handle($request, Closure $next)
    {

        if (! $request->user()->identification()->verified) {

            return redirect('identification');

        }
        return $next($request);
    }
}

我希望这应该返回为真并继续到下一页,因为我已经有属于该用户的标识表中的数据

但我收到此错误

ErrorException (E_NOTICE) 未定义属性: Illuminate\Database\Eloquent\Relations\HasOne :: $verified

【问题讨论】:

  • ErrorException (E_NOTICE) 未定义属性:Illuminate\Database\Eloquent\Relations\HasOne :: $verified
  • 请在此处发布您的型号代码(关系方法)。
  • public function identification(){ return $this-&gt;hasOne('App\Identification'); }
  • public function user(){ return $this-&gt;belongsTo('App\User'); }

标签: php laravel


【解决方案1】:

我猜问题出在你的 if 语句上,应该是这样的:
if (! $request-&gt;user()-&gt;identification-&gt;verified)

【讨论】:

  • 是的。 identification() 是一个 Eloquent 查询构建器。 identification 是查询result。人们在 Laravel 中常犯的错误。
  • 但我的目标是检查识别表中是否存在记录if (! $request-&gt;user()-&gt;exists('identification'))
  • @KingKesh 你应该可以做到if($request-&gt;user()-&gt;identification) {}。
猜你喜欢
  • 2021-07-24
  • 1970-01-01
  • 1970-01-01
  • 2019-10-04
  • 2021-09-27
  • 2021-09-15
  • 1970-01-01
  • 2019-03-28
  • 2021-07-19
相关资源
最近更新 更多