php - Laravel,使用中间件查找域,然后使用域行获取网站行。

标签 php laravel

我想使用一些中间件从 website 表中获取一行,该行在我的 domains 表中被引用。

到目前为止,这是我的中间件:

<?php

    namespace App\Http\Middleware;

    use App\Domains;
    use Closure;

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

            $domainRow = Domains::where([ 'domain' => $_SERVER['HTTP_HOST'] ])->first();

            dd($domainRow->website);

            return $next($request);
        }
    }

这是我的 Domains 模型,除了 hasOne 部分外,它工作正常:

<?php

    namespace App;

    use Illuminate\Database\Eloquent\Model;

    class Domains extends Model
    {

        public function website()
        {
            return $this->hasOne('App\Website');
        }

    }

hasOne 部分生成以下查询:

Unknown column 'websites.domains_id' in 'where clause' (SQL: select * from websites where websites.domains_id = 2 and websites.domains_id is not null limit 1)

但是我需要它进行的查询是:

SELECT * FROM websites WHERE id = 1

其中 1 来自 Domains 表中的 website_id 列。

最佳答案

将网站方法更改为:

public function website() 
{
    return $this->belongsTo('App\Website');
}

关于php - Laravel,使用中间件查找域,然后使用域行获取网站行。,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52506961/

相关文章:

php - 如何在 Windows 7 + PHP 5.6.1 上安装 memcached

php - mail() 函数不起作用 - 如何解决?

mysql - Laravel 4 MySQL 查询数组?

javascript - 上传前删除图片预览

php - 具有 Web 前端和移动应用程序的 Laravel 应用程序

laravel - 如何通过 Laravel 迁移创建外键?

php - 适用于 WordPress 的 Cat + 标签过滤器插件 : Checkboxes not being saved

php - 解析YouTube feed中的XML(无Zend)

javascript - 如果用户在文本字段中输入值并直接按浏览器刷新按钮,onblur 事件不起作用?

php - Laravel 5.1 - 操作多个数据库