这是一个非常奇怪的错误,也许我忽略了一些东西:我正在运行这个查询:
$getUnsupportedOS = Nessus::select('nessus_tags.unsupported_os')
->where('nessus_tags.unsupported_os', '=', 'true')
->join('nessus_tags','nessus_results.tagID','=','nessus_tags.tagID')
->whereRaw('nessus_results.scan_end >= UNIX_TIMESTAMP((NOW() - INTERVAL '.'1'.' MONTH))')
->count();
但它给我这个错误:
SQLSTATE[42S22]: Column not found: 1054 Unknown column 'nessus_tags.unsupported_os' in 'where clause' (SQL: select count(*) as aggregate from `nessus_results` inner join `nessus_tags` on `nessus_results`.`tagID` = `nessus_tags`.`tagID` where `nessus_tags`.`unsupported_os` != and nessus_results.scan_end >= UNIX_TIMESTAMP((NOW() - INTERVAL 1 MONTH)))
但是,正如您在所附图片中看到的(“列存在”),该表确实存在。我缺少什么?
在此先感谢您的帮助。
最好的问候!
最佳答案
将模型与表格连接通常是一个危险信号。相反,您应该创建一个相关模型并定义一个关系:
class Nessus {
protected $table='nessus_results';
//model
public function tags() {
return $this->hasMany(NessusTag::class, 'tagID');
}
}
然后你可以这样做:
$getUnsupportedOS = Nessus::withCount([ "tags" => function ($q) {
return $q->where("unsupported_os","=","true");
}])->whereRaw('scan_end >= UNIX_TIMESTAMP((NOW() - INTERVAL '.'1'.' MONTH))')->first();
然后计数在:$getUnsupprtedOS->tags_count
关于php - Laravel 5.4 SQLSTATE[42S22] : Column not found (but the column exists! ),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44470528/