简体   繁体   English

Laravel高级关系

[英]Laravel Elequent relationhips

I am pulling my hair out here, I have to almost identical elequent queries. 我在这里拔头发,我不得不进行几乎相同的询问。 They should be bringing back similar results, but they are not. 他们应该带来类似的结果,但事实并非如此。

Take note of the ReportsData in both controllers and the difference in the output, specifically the reports_data":[] relationships. the first example it outputs as expected, the second does not grab anything. 请注意两个控制器中的ReportsData以及输出中的差异,特别是reports_data“:[]关系。第一个示例按预期输出,第二个示例未捕获任何内容。

I hope someone can shed some light on this, I have been pulling my hair out for hours now :( 我希望有人能对此有所了解,我已经把头发拉了好几个小时了:(

Here is my code 这是我的代码

Controller 控制者

    $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();

$Report outputs the following $ Report输出以下内容

[{"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 Outputs the follwoing $ 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 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 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 report_data表

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 report_report表

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 table report_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

FIXED!! 固定!!

ReportsRepor required the foreign_key other_key ReportsRepor需要Foreign_key other_key

public function ReportsData(){
    return $this->hasMany('App\reportsData', 'suspect_id', 'suspect_id');
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM