Reputation: 17
My view:
My controller:
$user_id = auth()->user()->id;
$user = User::find($user_id);
$applications = Application::where('leaveStatus','APPROVED', 'REJECTED')->get();
return view('applications.index')->with('applications', $user->applications);
How do I display approved or rejected leave status only
Upvotes: 1
Views: 951
Reputation: 1
There are 2 ways :-
Raw Query Structure :-
select * from TABLENAME where COLUMNNAME = 'VALUE1' or COLUMNNAME = 'VALUE2';
In this case VALUE1 = 'APPROVED', VALUE2 = 'REJECTED', COLUMNNAME = 'leaveStatus'
Laravel Query :-
$applications = Application::where('leaveStatus','APPROVED') ->orWhere('leaveStatus','REJECTED')->get();
Raw Query Form :-
select * from TABLENAME where COLUMNNAME in ("VALUE1","VALUE2")
In this case VALUE1 = 'APPROVED', VALUE2 = 'REJECTED', COLUMNNAME = 'leaveStatus'
Laravel Query :-
$applications = Application::whereIn('leaveStatus',['APPROVED', 'REJECTED'])->get();
Upvotes: 0
Reputation: 2951
You can use the whereIn
method for that purpose and pass your $applications
to the view:
$applications = Application::whereIn('leaveStatus',['APPROVED', 'REJECTED'])->get();
return view('applications.index', compact('applications'));
As per the documentation:
The whereIn method verifies that a given column's value is contained within the given array
Upvotes: 3
Reputation: 8252
Your code doesn't make much sense at the moment:
auth()
helper$applications
variableWhat you could do instead is:
$applications = auth()->user()
->applications()
->whereIn('leaveStatus', ['APPROVED', 'REJECTED'])
->get();
return view('applications.index', compact('applications'));
What we do here is get the authenticated user, load their applications where the leaveStatus
is APPROVED
or REJECTED
and then pass it to the view with the help of compact:
compact('applications')
=== ['applications' => $applications]
From the docs:
The whereIn method verifies that a given column's value is contained within the given array:
$users = DB::table('users')
->whereIn('id', [1, 2, 3])
->get();
Upvotes: 1