我要把头发拉出来,我几乎要完全相同的问题。他们应该带回类似的结果,但事实并非如此。
记下两个控制器中的 ReportsData 以及输出的差异,特别是 reports_data":[] 关系。它按预期输出的第一个例子,第二个没有抓住任何东西。
我希望有人可以对此有所了解,我已经把头发拉了几个小时了:(
这是我的代码
控制器
$report = ReportsSuspect::where('id', '=', $id)
->with('serverModel')
->with('reportsReportModel')
->with('reportsReporter')
->with('reportsData')
->get();
$reports = ReportsReport::where('suspect_id', '=', $id)
->with('serverModel')
->with('reportsSuspect')
->with('reportsReporter')
->with('reportsData')
->get();
$报告输出以下内容
[{"id":4,"steamid":"0987654321","assigned":0,"archived":0,"created_at":"2017-11-30 14:03:45","updated_at":"2017-11-30 14:03:45","server_model":null,"reports_report_model":[{"id":9,"name":"hackerName","offence":"Hacking-ESP\/AIM, Toxic Behaviour","server":25,"suspect_id":4,"reporter_id":3,"created_at":"2017-11-30 14:03:45","updated_at":"2017-11-30 14:03:45"}],"reports_reporter":null,"reports_data":[{"id":16,"reporter_id":3,"suspect_id":4,"moderator_id":0,"comment":null,"url":"image1","comment_type":0,"url_type":1,"created_at":"2017-11-30 14:03:45","updated_at":"2017-11-30 14:03:45"},{"id":17,"reporter_id":3,"suspect_id":4,"moderator_id":0,"comment":null,"url":"video1","comment_type":0,"url_type":2,"created_at":"2017-11-30 14:03:45","updated_at":"2017-11-30 14:03:45"},{"id":18,"reporter_id":3,"suspect_id":4,"moderator_id":0,"comment":"I saw this guy hacking","url":null,"comment_type":1,"url_type":0,"created_at":"2017-11-30 14:03:45","updated_at":"2017-11-30 14:03:45"}]}]
$ reports输出以下
[{"id":9,"name":"hackerName","offence":"Hacking-ESP\/AIM, Toxic Behaviour","server":25,"suspect_id":4,"reporter_id":3,"created_at":"2017-11-30 14:03:45","updated_at":"2017-11-30 14:03:45","server_model":{"id":25,"servername":"Rustafied Long","serverlocation":"US","available":1,"servertextname":"US - Long","servertype":1,"serverconnection":"","serverinfo":"","serverpopulation":"","created_at":null,"updated_at":null},"reports_suspect":{"id":4,"steamid":"0987654321","assigned":0,"archived":0,"created_at":"2017-11-30 14:03:45","updated_at":"2017-11-30 14:03:45"},"reports_reporter":{"id":3,"steamid":"76561198363172919","name":"Cake","created_at":"2017-11-30 09:21:40","updated_at":"2017-11-30 09:21:40"},"reports_data":[]}]
ReportsSuspectController
// Relationship data
public function reportsReport(){
return $this->belongsTo('App\reportsReport', 'reporter_id');
}
public function reportsData(){
return $this->hasMany('App\reportsData', 'suspect_id');
}
public function reportsReportModel(){
return $this->hasMany('App\reportsReport', 'suspect_id');
}
public function reportsReporter(){
return $this->belongsTo('App\reportsReporter', 'id');
}
public function serverModel(){
return $this->belongsTo('App\server', 'server');
}
ReportsReportController
// Relationship data
public function reportsSuspect(){
return $this->belongsTo('App\reportsSuspect', 'suspect_id');
}
public function reportsReporter(){
return $this->belongsTo('App\reportsReporter', 'reporter_id');
}
public function ReportsData(){
return $this->hasMany('App\reportsData', 'suspect_id');
}
public function serverModel(){
return $this->belongsTo('App\Server', 'server');
}
public function offenceModel(){
return $this->belongsTo('App\Offence', 'offence');
}
reports_data table
id int(10) No None
reporter_id int(11) Yes NULL
suspect_id int(11) Yes NULL
moderator_id int(11) No 0
comment text utf8_unicode_ci Yes NULL
url text utf8_unicode_ci Yes NULL
comment_type int(11) Yes 0
url_type int(11) Yes 0
created_at timestamp Yes NULL
updated_at timestamp Yes NULL
reports_report table
id i nt(10) No None
name text utf8_unicode_ci Yes NULL
offence text utf8_unicode_ci Yes NULL
server int(11) Yes NULL
suspect_id int(11) Yes NULL
reporter_id int(11) Yes NULL
created_at timestamp Yes NULL
updated_at timestamp Yes NULL
reports_suspect表
idPrimary int(10) No None
steamid varchar(25) utf8_unicode_ci Yes NULL
assigned int(11) Yes 0
archived int(11) No 0
created_at timestamp Yes NULL
updated_at timestamp Yes NULL
答案 0 :(得分:0)
固定!!
ReportsRepor 需要foreign_key other_key
public function ReportsData(){
return $this->hasMany('App\reportsData', 'suspect_id', 'suspect_id');
}