PHP变量未传递到具有功能的地方-Laravel Eloquent

Nic*_*ick 3 php laravel eloquent laravel-5

简介:我无法在whereHas函数(如下)中访问$ domain变量;Laravel返回以下错误:

“未定义的变量:域”

我之所以加入我的人际关系,是因为我不确定雄辩的性质以及我如何尝试调用此查询是否会引起问题。


我有一个称为模型(Org)的中间件

组织模型有字段

子域

组织模型有

public function domains()
{
    return $this->hasMany('App\Domain');
}
Run Code Online (Sandbox Code Playgroud)

模型(表名域)具有字段

域,org_id

也有功能

public function org()
{
    return $this->belongsTo('App\Org');
}
Run Code Online (Sandbox Code Playgroud)

我可以dd($domain); 在此功能之前没有任何问题。但是,我正在

“未定义的变量:域”

下面的whereHas函数中的查询参数。

laravel为什么看不到上面设置的变量?

namespace App\Http\Middleware;
use Closure;
class DomainLookup
{

    protected $org;

    /**
     * Handle an incoming request.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Closure  $next
     * @return mixed
     */
    public function handle($request, Closure $next)
    {
        $route = $request->route();
        $domain = $route->parameter('domain');
        $trimmed_domain = trim($domain, (config('app.domain')));

        $this->org = \App\Org::where('subdomain',$trimmed_domain)
             ->whereHas('domains', function($q) {
                  $q->where('domain',  $domain);
              })
              ->get();

        if ($this->org) {
            return $next($request);
        }

    }


}
Run Code Online (Sandbox Code Playgroud)

Lae*_*rte 7

您需要调用useafter函数:

$this->org = \App\Org::where('subdomain',$trimmed_domain)
             ->whereHas('domains', function($q) use ($domain) {
                  $q->where('domain',  $domain);
              })
              ->get();
Run Code Online (Sandbox Code Playgroud)