OK I have a system that generates subscriptions from customers related to
- Service,.
- Account (email) belonging to that service.
- Profile related to the account.
All this is associated to a client, but to be able to see it at the time of the subscription all this keeps relation to the authenticated user.
The problem I have it when obtaining the profiles.
I have functions to get the accounts associated to a service_id, so I can get the profiles associated to an account_id.
When an account has sale_type = ‘complete’; its associated profiles are linked through the account_id which in turn has a user_id (account holder) in the accounts table.
But when an account has sale_type = ‘profile”; its profiles are associated to the user through a field called user_id in the profiles table.
In this way; if an account is sale_type = ‘complete and you want to make a subscription, you will get all the profiles associated to that account, while if an account is sale_type = ‘profile’ you will get only the profiles belonging to that account in which the user_id (from the profiles table) is the authenticated user.
In the following image I can select the Service, I can select a mail (mabeliñ[email protected]) with the condition sale_type = ‘complete’, and then I can see the profiles it has.
But when I choose an account with the condition sale_type = ‘profile’, it does not show me the profiles that the account has on behalf of the authenticated user.
I have the function below that shows me the profiles in the accounts sale_type= ‘complete’ but does not show the profiles in the accounts sale_type= ‘profile’.
public function getProfiles($account_id){
$data = [];
// Obtener la cuenta con el id de cuenta dado
$account = Account::find($account_id);
// Comprobar si la cuenta existe
if ($account) {
$attr = [
'table' => 'profiles',
'columns' => [
'id',
'name',
'pin',
'user_id' // Agregar user_id para obtener este campo también
],
'compare' => 'account_id',
'compare_value' => $account_id
];
$response = Helper::getDataSelect($attr);
// Verificar el sale_type de la cuenta
if ($account->sale_type == 'complete') {
// Si sale_type es 'complete', mostrar todos los perfiles
$data = $response;
} else if ($account->sale_type == 'profile') {
// Si sale_type es 'profile', mostrar solo los perfiles para el usuario actual
if (count($response) > 0){
foreach ($response as $res){
$profile = Profile::find($res->id);
if ($profile && $profile->subscriptions->count() == 0) {
if ($profile->user_id == Auth::user()->id) {
array_push($data, $res);
}
}
}
}
}
}
return response()->json(['data' => $data]);
}
I have tried changing the function to this:
public function getProfiles($account_id){
$data = [];
$attr = [
'table'=>'profiles',
'columns'=>[
'id',
'name',
'pin'
],
'compare'=>'account_id',
'compare_value'=>$account_id
];
$response = Helper::getDataSelect($attr);
if(count($response) > 0){
foreach($response as $res){
$profile = Profile::find($res->id);
if($profile->subscriptions->count() == 0){
if($profile->user_id == Auth::user()->id){
array_push($data, $res);
}
}
}
}
return response()->json(['data'=>$data]);
}
This way it lets me see the profiles of the account sale_type= ‘profile’ but it does not let me see the profiles of the accounts sale_type=’complete’ 🙁
I can see the profiles that belong to me
and for the ‘full’ account I don’t see the profiles
I need help, to allow me to show the profiles depending on the sale_type, because currently if I use one method I can see the sale_type=’complete’ and the others not. and if I change the function I can see the accounts sale_type=’profile’ and the others not 🙁
esta es mi funcion en el Helper:
public static function getDataSelect($attr, $validateUser=false){
if($attr){
if($validateUser){
$data = DB::table($attr['table'])->select($attr['columns'])->where($attr['compare'],$attr['compare_value'])->where('user_id',Auth::user()->id)->get();
}else{
$data = DB::table($attr['table'])->select($attr['columns'])->where($attr['compare'],$attr['compare_value'])->get();
}
return $data;
}else{
return null;
}
}
2
Answers
I solved the problem this way
What if you use the filtering for your second scenario based on authenticated user?
Here is your code with modifications:
I hope this help you.