Php 具有返回不准确结果的关系的Laravel
我正在尝试进行查询,以获取所有面试,并在companys表中获取公司名称。我只想返回company表中的name列 我运行了一个查询日志,发现我的关系返回了一个奇怪的查询 Laravel版本6.5.2 我原来拥有的Php 具有返回不准确结果的关系的Laravel,php,laravel,laravel-5,eloquent,Php,Laravel,Laravel 5,Eloquent,我正在尝试进行查询,以获取所有面试,并在companys表中获取公司名称。我只想返回company表中的name列 我运行了一个查询日志,发现我的关系返回了一个奇怪的查询 Laravel版本6.5.2 我原来拥有的 $interviews = Interview::select('title', 'slug', 'subtitle', 'posted_date') ->orderBy('created_at', 'DE
$interviews = Interview::select('title', 'slug', 'subtitle', 'posted_date')
->orderBy('created_at', 'DESC')
->with(['company' => function($query) {
$query->select('id','name');
}])
->get();
我现在用什么来测试关系是否正常:
\DB::enableQueryLog();
$interviews = Interview::select('title', 'slug', 'subtitle', 'posted_date')
->orderBy('created_at', 'DESC')
->with('company')
->get();
dd(\DB::getQueryLog());
public function interview() {
return $this->hasOne('App\Interview', 'company_id', 'id');
}
public function company() {
return $this->belongsTo('App\Company', 'id', 'company_id');
}
select `title`, `slug`, `subtitle`, `posted_date` from `interviews` where `interviews`.`deleted_at` is null order by `created_at` desc
select * from `companies` where 0 = 1 and `companies`.`deleted_at` is null
公司模式:
\DB::enableQueryLog();
$interviews = Interview::select('title', 'slug', 'subtitle', 'posted_date')
->orderBy('created_at', 'DESC')
->with('company')
->get();
dd(\DB::getQueryLog());
public function interview() {
return $this->hasOne('App\Interview', 'company_id', 'id');
}
public function company() {
return $this->belongsTo('App\Company', 'id', 'company_id');
}
select `title`, `slug`, `subtitle`, `posted_date` from `interviews` where `interviews`.`deleted_at` is null order by `created_at` desc
select * from `companies` where 0 = 1 and `companies`.`deleted_at` is null
面试模式:
\DB::enableQueryLog();
$interviews = Interview::select('title', 'slug', 'subtitle', 'posted_date')
->orderBy('created_at', 'DESC')
->with('company')
->get();
dd(\DB::getQueryLog());
public function interview() {
return $this->hasOne('App\Interview', 'company_id', 'id');
}
public function company() {
return $this->belongsTo('App\Company', 'id', 'company_id');
}
select `title`, `slug`, `subtitle`, `posted_date` from `interviews` where `interviews`.`deleted_at` is null order by `created_at` desc
select * from `companies` where 0 = 1 and `companies`.`deleted_at` is null
结果:
\DB::enableQueryLog();
$interviews = Interview::select('title', 'slug', 'subtitle', 'posted_date')
->orderBy('created_at', 'DESC')
->with('company')
->get();
dd(\DB::getQueryLog());
public function interview() {
return $this->hasOne('App\Interview', 'company_id', 'id');
}
public function company() {
return $this->belongsTo('App\Company', 'id', 'company_id');
}
select `title`, `slug`, `subtitle`, `posted_date` from `interviews` where `interviews`.`deleted_at` is null order by `created_at` desc
select * from `companies` where 0 = 1 and `companies`.`deleted_at` is null
我试图弄清楚为什么它返回,其中0=1
,而不是实际的模型
另外,是否有一种方法可以只返回上面类似I的字段之一?找到了答案。您必须在原始查询中指定
id
和company\u id
,才能使其正常工作
$interviews = Interview::select('id', 'title', 'company_id', 'slug', 'subtitle', 'posted_date')
->orderBy('created_at', 'DESC')
->with(['company:id,name'])
->get();
看看这个: