Laravel 用户和管理员之间的简单消息系统
Simple messages system between user and admin with Laravel
我在用户和管理员之间创建了简单的消息系统。当用户报告某些项目时,我存储了他的报告,然后管理员有此报告的按钮 'Replay to user'。
我想我搞砸了数据库 tables 和关系所以请帮助我理解这一点。
所以我有 table reports
列 + 示例数据(两个用户报告了相同的项目)
report_id | user_id | item_id
1 1 1
1 2 1
此 table 包含用户 (user_id) 对给定项目 (item_id) 的报告。然后我有 table 调用 reports_messages
列 + 用户和管理员之间的重播示例
report_id | user_id | item_id | messages
1 1 1 Report
1 admin 1 answer from admin
1 2 1 Report from other user
1 admin 1 answer from admin to other user
这是我的ReportMessages
模型
class ReportMessages extends Model
{
protected $table = 'reports_messages';
protected $fillable = [
'report_id', 'user_id', 'messages', 'item_id'
];
public function reports(){
return $this->belongsTo('App\Report');
}
public function users()
{
return $this->hasOne('App\User', 'id', 'user_id');
}
}
在管理员控制器中,这就是我显示给定 item_id
的所有报告的方式
public function details( $item_id ){
$flags = Item::find($item_id)->report->unique('user_id');
return view('details', compact('flags'));
}
按钮重播给用户
<a href="{{URL::to('replays/' . collect($flags)->first()->item_id)}}">Replay To User</a>
和路线
Route::get('details/{details}', 'FlagsController@details')->name('details');
问题:当我有 2 个用户报告了相同的项目并且我单击“重播给用户”按钮时,无论我打开哪个用户,我都会收到两个用户的消息。
我知道问题可能出在按钮上,因为我添加了 collect($flags)->first()->item_id
,它给我的是项目 ID 而不是用户 ID,但我无法弄清楚这里的关系以及当我点击重播给用户时如何使用id=1 只加载他的消息。
更新:物品模型
public function report(){
return $this->hasMany('App\Report', 'item_id','id');
}
在你的控制器中
public function details( $item_id ){
$flags = Item::where('item_id',$item_id)->get();
return view('details', compact('flags'));
}
在您看来
foreach($flags as $flag):
<a href="{{URL::to('replays/' . $flag->item_id.'/'.$flag->user_id)}}">Replay To User</a>
endforeach;
我在用户和管理员之间创建了简单的消息系统。当用户报告某些项目时,我存储了他的报告,然后管理员有此报告的按钮 'Replay to user'。
我想我搞砸了数据库 tables 和关系所以请帮助我理解这一点。
所以我有 table reports
列 + 示例数据(两个用户报告了相同的项目)
report_id | user_id | item_id
1 1 1
1 2 1
此 table 包含用户 (user_id) 对给定项目 (item_id) 的报告。然后我有 table 调用 reports_messages
列 + 用户和管理员之间的重播示例
report_id | user_id | item_id | messages
1 1 1 Report
1 admin 1 answer from admin
1 2 1 Report from other user
1 admin 1 answer from admin to other user
这是我的ReportMessages
模型
class ReportMessages extends Model
{
protected $table = 'reports_messages';
protected $fillable = [
'report_id', 'user_id', 'messages', 'item_id'
];
public function reports(){
return $this->belongsTo('App\Report');
}
public function users()
{
return $this->hasOne('App\User', 'id', 'user_id');
}
}
在管理员控制器中,这就是我显示给定 item_id
的所有报告的方式public function details( $item_id ){
$flags = Item::find($item_id)->report->unique('user_id');
return view('details', compact('flags'));
}
按钮重播给用户
<a href="{{URL::to('replays/' . collect($flags)->first()->item_id)}}">Replay To User</a>
和路线
Route::get('details/{details}', 'FlagsController@details')->name('details');
问题:当我有 2 个用户报告了相同的项目并且我单击“重播给用户”按钮时,无论我打开哪个用户,我都会收到两个用户的消息。
我知道问题可能出在按钮上,因为我添加了 collect($flags)->first()->item_id
,它给我的是项目 ID 而不是用户 ID,但我无法弄清楚这里的关系以及当我点击重播给用户时如何使用id=1 只加载他的消息。
更新:物品模型
public function report(){
return $this->hasMany('App\Report', 'item_id','id');
}
在你的控制器中
public function details( $item_id ){
$flags = Item::where('item_id',$item_id)->get();
return view('details', compact('flags'));
}
在您看来
foreach($flags as $flag):
<a href="{{URL::to('replays/' . $flag->item_id.'/'.$flag->user_id)}}">Replay To User</a>
endforeach;