如何修复Laravel关系查询where子句?

时间:2019-03-26 21:40:25

标签: laravel eloquent relationship

我试图通过“ computer_files”数据透视表从“文件”表中获取数据,但是查询将“ is null”放入查询中,因此它不随数据一起返回。但是,如果我将null更改为ID,然后将其作为SQL命令运行,则确实会返回数据。

我曾经尝试过laravel文档,但这并没有帮助。我不想做的就是使用原始查询。

这是文件关系功能:

public function files(){
        return $this->belongsToMany('App\File', 'computer_files', 'computer_id', 'file_id');
}

这是我要获取的数据,当前会返回一个空集合:

$computer = Computer::where('computer_id', session('computer_id'))->first();
pr($computer->files);
die;

“ toSql()”也给出了这一点:

select * from `files` inner join `computer_files` on `files`.`id` = `computer_files`.`file_id` where `computer_files`.`computer_id` is null

我希望在查询中看到一个ID而不是'is null',所以它将返回我需要的结果。

0 个答案:

没有答案
相关问题