Laravel一对多关系查询绑定显示为空?

问题描述:

我有以下表格。 Laravel公约没有被遵守。 表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)它不返回任何记录。结合PARAMS显示空

当调试查询它显示了以下

1 => array:3 [▼ 
    "query" => "select * from `helpdesk_files` where `helpdesk_files`.`helpdesk_logs_id` in (?)" 
    "bindings" => array:1 [▼ 
     0 => null 
    ] 
    "time" => 0.17 
    ] 

在您的帮助台记录表ID colunn可以用来检索所需的结果 HelpdeskLogs::with('helpdeskFiles')->where('id', 13)->get();

+0

对不起,这是错字实际(而拷贝)我还有其他专栏helpdesk_id。我在helpdesk_files表中有helpdesk_log_id 13的记录 –

+0

@ kamlesh.bar您可以在您的问题中发布正确的代码。我无法理解这个问题。:( – jaysingkar

+1

感谢@jaysingkar问题已解决。问题在于'select'语句没有id(helpdesk_logs.id)我已经添加了helpdesk_logs.id AS helpdesk_logs_id –