Reputation: 445
I am using laravel cashier version 9 with laravel 5.8, stripe subscriptions are done properly and each user can see its own invoices on the user dashboard page.
Basically we are using two types of users on our website, one is a normal user and another one is admin, In normal user dashboard we are displaying invoices and it is working properly by using the below code.
public function index()
{
try {
$invoices = Auth::user()->invoicesIncludingPending();
} catch ( \Exception $e ) {
session()->flash('status', $e->getMessage());
}
return view('invoice', compact('invoices'));
}
Now we want at the admin dashboard end to list and display all the invoices for all the users so admin can see all the invoices data for all users in the admin dashboard. Can you please help me, how I can achieve this in the admin dashboard.
Upvotes: 1
Views: 704
Reputation: 11
I think you have already done the 80% job now follow the instruction as I don't have complete information of your model and relation this answer is assumptions only..
public function socpeInvoice($query)
{
//put this type of function in your invoice model
//make a relation in your user model fro user Invoice like this the bleow commented function
/* public function invoice()
{
return $this->hasMany(Invoice::class, 'user', 'id');
} */
//check for the condition how you get the only pending invoice hear i have used status as pending. you can check in your condition,
if(Auth::user() == 'Admin'){
User::whereHas('invoice',function($query){
$query->where('status','pending');
})->orderBy('id','asc'); //check in your condition how you get the admin user
}else{
Auth::user()->invoicesIncludingPending();//you can call your function hear
}
//As i dont know the logic inside invoicesIncludingPending() function i can only help you out till this extent
}
This is the assumptions based answer as I don't know about your model and functions
On Your Controller
public function index()
{
try {
$invoices = User::Invoice()->get();//or paginate
} catch ( \Exception $e ) {
session()->flash('status', $e->getMessage());
}
return view('invoice', compact('invoices'));
}
Upvotes: 1