Laravel One to Many Relation查询绑定显示为null?

时间:2016-09-07 07:04:19

标签: php mysql laravel laravel-5 eloquent

我有以下表格。没有遵循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)它没有返回任何记录。 binding params显示为null

当我调试查询时,它显示以下

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

1 个答案:

答案 0 :(得分:0)

在您的服务台日志表中,id colunn可用于检索所需的结果 HelpdeskLogs::with('helpdeskFiles')->where( 'id', 13)->get();