I want to show a list of complaint of a logged-in user how would I write if else in a blade.
JavaScript
x
@foreach($data as $row)
<tr>
<td>{{ $row->type }}</td>
<td>{{ $row->station }}</td>
<td>{{ $row->description }}</td>
<td>{{ $row->comment }} </td>
<td>{{ $row->status }}</td>
<td class="text-center">
<a href="{{ route('view-complaint', $row->id) }}" class="btn btn-primary btn-sm"><i class="fa fa-eye mr-1" aria-hidden="true"></i>Show</a>
<a href="{{ route('edit-complaint', $row->id) }}" class="btn btn-warning btn-sm"><i class="fa fa-pencil mr-1" aria-hidden="true"></i>Edit</a>
<form action="{{ route('delete-complaint', $row->id) }}" method="post" class="d-inline-block">
@csrf
@method('DELETE')
<button type="submit" class="btn btn-danger btn-sm"><i class="fa fa-trash mr-1" aria-hidden="true"></i>Delete</button>
</form>
</td>
</tr>
@endforeach
and this my index function, it also works for admin to show all complaints of all users. But I want the complaints for a specific user who logged in. I mean the user can see all of his complaints
JavaScript
public function index()
{
$data = Complaint::latest()->paginate(5);
return view('pages.allComplaints', compact('data'))->with('i', (request()->input('page', 1) - 1) * 5);
}
How would I handle this!
Advertisement
Answer
Change the Index method like this:
JavaScript
public function index()
{
if(auth()->user()){
$data = Complaint::latest()->where('user_id',auth()->user()->id)->paginate(5);
}else{
$data = Complaint::latest()->paginate(5);
}
return view('pages.allComplaints', compact('data'))->with('i', (request()->input('page', 1) - 1) * 5);
}