我有下表。不遵循Laravel约定。
桌子
helpdesk_logs (table)
id primary_key
helpdesk_files (table)
id primary_key
helpdesk_logs_id - foreign_key
模型
HelpdeskLogs.php
public function helpdeskFiles(){
return $this->hasMany( 'Defsys\Modules\helpdesk\Models\HelpdeskFiles','helpdesk_logs_id','id');
}
HelpdeskFiles.php
public function helpdeskLogs()
{
return $this->belongsTo( 'Defsys\Modules\helpdesk\Models\HelpdeskLogs','helpdesk_logs_id' );
}
我正在执行以下任务
HelpdeskLogs::with('helpdeskFiles')
->select( DB::raw( 'helpdesk_logs.id AS helpdesk_logs_id,helpdesk_logs.description,helpdesk_logs.status ,helpdesk_logs.spent_time' ) )
->where( 'helpdesk_logs.id', 13)->get();
尽管在helpdesk_files中有记录(对于helpdesk_lod_id 13),但它不返回任何记录。绑定参数显示为空
当我调试查询时,它显示以下内容
1 => array:3 [▼
"query" => "select * from `helpdesk_files` where `helpdesk_files`.`helpdesk_logs_id` in (?)"
"bindings" => array:1 [▼
0 => null
]
"time" => 0.17
]
最佳答案
在您的帮助台日志表中,id列可用于检索所需结果HelpdeskLogs::with('helpdeskFiles')->where( 'id', 13)->get();