user13532318
user13532318

Reputation: 17

How to display approved or rejected leave status?

My view:

enter image description here

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

Answers (3)

Sahil Gaonkar
Sahil Gaonkar

Reputation: 1

There are 2 ways :-

  1. Using orWhere()

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

  1. Using whereIn()

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

Christophe Hubert
Christophe Hubert

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

Remul
Remul

Reputation: 8252

Your code doesn't make much sense at the moment:

  1. No need to fetch the user from the database when you are already getting them from the auth() helper
  2. You are not doing anything with the $applications variable

What 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

Related Questions