最近我开始学习laravel,目前我正在使用5.2版本。 我所拥有的是标记/报告系统,用户可以出于各种原因报告项目,并将报告保存在数据库中。到目前为止,这很有效。我现在想要在后端(管理仪表板)中选择报告以查看其详细信息,以查看报告此特定项目的所有用户。
到目前为止,我只看到一个用户,但我无法弄清楚如何制作模型和查询。
所以我的Item模型有
public function report(){
return $this->belongsTo('App\Report');
}
报告模式
public function reportedItem(){
return $this->hasMany('Item','item_id');
}
在控制器中
public function details( $report_id ){
$flags = Report::where('report_id', '=', $report_id)->get();
return view('details', compact('flags'));
}
在表格reports
中,一个项目可以包含许多用户的许多报告。感谢您的任何帮助。
更新。
模型报告
public function reportedItem(){
$this->hasMany('App\Item', 'id', 'item_id');
}
控制器
public function details( $item_id ){
$flags = Item::find($item_id)->reportedItem;
return view('details', compact('flags'));
}
按钮
{!! Form::open(array('route' => array('details', $flag->item_id), 'method' => 'get')) !!}
<button>Details</button>
{!! Form::close() !!}
in details.blade
@foreach($flags as $flag)
....
@endforeach
答案 0 :(得分:1)
在您的控制器中更改您的代码
public function details( $report_id ){
$flags = Report::where('report_id', '=', $report_id)->get();
return view('details', compact('flags'));
}
到
public function details( $item_id ){
$flags = App\Item::find($item_id)->report;
return view('details', compact('flags'));
}
之后,您可以访问每个细节,如
foreach ($flags as $flag) {
//
}
修改强>
您的Report
模式更改中的
public function reportedItem(){
$this->hasMany('App\Item', 'item_id','id');
}
到
public function reportedItem(){
return $this->belongsTo('App\Item');
}
和项目模型
public function report(){
return $this->belongsTo('App\Report');
}
到
public function report(){
return $this->('App\Report', 'item_id','id');
}