php - Laravel, use middleware to find Domain, then get Website row using Domain row.
问题描述
I want to use some middleware to get a row from a website
table, this row is referenced within my domains
table.
This is my middleware so far:
<?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);
}
}
And here is my Domains
model, which works fine except the hasOne part:
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Domains extends Model
{
public function website()
{
return $this->hasOne('App\Website');
}
}
The hasOne part is generating the below query:
Unknown column 'websites.domains_id' in 'where clause' (SQL: select * from
websites
wherewebsites
.domains_id
= 2 andwebsites
.domains_id
is not null limit 1)
However the query I need it to make is:
SELECT * FROM websites WHERE id = 1
Where 1
is from the website_id
column within the Domains table.
解决方案
将网站方法更改为:
public function website()
{
return $this->belongsTo('App\Website');
}
推荐阅读
- emeditor - 在 EmEditor 中:如何对两列单元格(在同一行)中的值求和并将结果存储在第三列中?
- laravel - 如何获取当前模块名称 [nwidart/laravel-modules]
- sql-server - SQL server 使用表参数运行作业
- postgresql - 查询以获取 OS 进程(postmaster)并在 postgres 数据库中与之关联的查询
- python-3.x - 如何在 Python 程序中使用正确的程序打开文件(例如,使用 Blender 的“.blend”和使用 Chrome 的“.webloc”)?
- .net - 我们可以从另一个模块调用一个模块的叶子设备直接方法吗?
- kubernetes - Openshift 路由重定向到集群控制台
- c# - 如何将gridview的内容打印到文本框
- html - 当弹性项目的填充以百分比给出时,为什么弹性容器不是弹性项目的宽度?
- ios - 具有自定义约束的 UIStackView - 更改轴时“无法同时满足约束”