简体   繁体   中英

Show record of logged in user in laravel

I want to show a list of complaint of a logged-in user how would I write if else in a blade.

@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

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!

Change the Index method like this:

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);
}

You can try if-else in blade as mentioned below:

@if($user->logged_in)         
      //show records    
@else
      //not logged in    
@endif

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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