I am very new into programming and trying to make some work on Laravel / Voyager.
I have a Customers table and on this table there is also Customer_Representative field where I can select / assign customer representatives from dropdown list.
What I am trying to achieve is I want each users with their assigned roles (Customer Representative) to list customers only assigned for them.
I am using this below code, but I am not able to see list of customers that other users created.
I can only see the customers I / Admin created but If I login with their details I can see their assigned customers.
So as an admin I want to be able to see all customers, so I can also edit them with my login credentials.
public function scopeCurrentUser($query)
{
return $query->where('Customer_Representative', Auth::user()->id);
}
I saw many suggested to use if but I couldn't figure out how to do it.
Appreciate your supports !
CodePudding user response:
If I understand correctly, you want to display:
- admin users - all users
- non-admin users - only assigned users
You can either change the query in controller, but if you want to do it in scope I suggest something like this:
public function scopeCurrentUser($query)
{
return $query->when(auth()->user()->is_not_admin, function ($q) {
$q->where('Customer_Representative', auth()->user()->id)
});
}
You change the auth()->user()->is_not_admin to your condition for non-admin validation. The scope utilizes the ->when() function to append the condition to query only if the condition (in this case if authenticated user is not an admin) is true.
So if authenticated user is not an admin, his query will be filtered by Customer_Representative.
CodePudding user response:
Lukas see my code below;
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\Auth;
class Customer extends Model
{
public function scopeCurrentUser($query)
{
return $query->when(auth()->user()->is_not_admin, function ($q) {
$q->where('Customer_Representative', auth()->user()->id)
});
}
}
result is like this;
ParseError
syntax error, unexpected token "}"
What I have been trying to achieve is;
I have Customers table
Customer Name | Customer Representative |
---|---|
Customer 1 | Customer Representative 1 |
Customer 2 | Customer Representative 2 |
so Customer 1 is assigned to Customer Representative 1, and Customer 2 is assigned to Customer Representative 2.
When lets say Customer Representative 1 logs in to backend panel and when he checks his customers table I want him to see only Customer 1 because thats the only assigned customer for him.
and as an admin I want to see both Customer 1 and Customer 2 so I can edit their details freely.
Below old code I was using filtering customers table and showing only assigned customers for each user / customer representative. But I am not able to see customers as an admin because they are assigned for other users - Customer Representative 1 and Customer Representative 2.
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\Auth;
class Customer extends Model
{
public function scopeCurrentUser($query)
{
return $query->where('Customer_Representative', Auth::user()->id);
}
}
Thank you for the support !