Reputation: 445
I've created simple message system between user and admin. When user report some item I have storing his report and then admin has button 'Replay to user' for this report.
I think I messed something with database tables and relations so please help me understand this.
So I have table reports
with columns + example data ( two users reported same item )
report_id | user_id | item_id
1 1 1
1 2 1
This table holds reports from user(user_id) to given item(item_id). Then I have table called reports_messages
with columns + example of replays between both users and admin
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
This is my ReportMessages
model
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');
}
}
In admin controller this is how I display all reports for given item_id
public function details( $item_id ){
$flags = Item::find($item_id)->report->unique('user_id');
return view('details', compact('flags'));
}
button replay to user
<a href="{{URL::to('replays/' . collect($flags)->first()->item_id)}}">Replay To User</a>
And route
Route::get('details/{details}', 'FlagsController@details')->name('details');
Problem: When I have 2 users reported same item and I click on Replay to user button I get messages of both user no matter which one I opened.
I know that the problem i probably the button because I added collect($flags)->first()->item_id
which gives me item ID not user ID but I can't figured out the relations here and how when I click to replay to user with id=1 to load only his messages.
Update: Item model
public function report(){
return $this->hasMany('App\Report', 'item_id','id');
}
Upvotes: 3
Views: 2837
Reputation: 764
In your Controller
public function details( $item_id ){
$flags = Item::where('item_id',$item_id)->get();
return view('details', compact('flags'));
}
In your view
foreach($flags as $flag):
<a href="{{URL::to('replays/' . $flag->item_id.'/'.$flag->user_id)}}">Replay To User</a>
endforeach;
Upvotes: 1