Laravel Filter using various relations - php

I am doing a filter using relations here is my controller code:
$userList = User::with(['role' ,'userMetaData','userBet','userComission','userPartnership'])
->where('deleted', '=', '0')
->when($request->parent_id, function ($builder, $parent_id) {
return $builder->where('parent_id', $parent_id);
})
->when($request->role_id, function ($builder, $role_id) {
return $builder->where('role', $role_id);
})
->when($request->to && $request->from , function ($builder) use ($request) {
return $builder->whereBetween('exposure_limit', [$request->from, $request->to]);
})
->when($request->city, function ($builder) use ($request) {
return $builder->whereHas('userMetaData', function($query) use ($request){
$query->where('city', $request->sport);
});
})
->orderBy('created_at', 'DESC')
->get();
In this case(in my code) i am trying to use whereHas() to filter a data from another table(within relations) by eloquent but it's something i am missing:

here in your case, when you use Wherehas to userMetaData it'll filter and gives you only those users who's has city as $request->sport..
In another case, you've used with('userMetaData') which means whatever users you got in filter return all userMetaData.
->when($request->city, function ($builder) use ($request) {
return $builder->whereHas('userMetaData', function($query) use ($request){
$query->where('city', $request->sport);
})->with(['userMetaData' => function($q) use ($request) {
$q->where('city', $request->sport);
}]);
})
Remove userMetaData from User::with(['role','userBet','userComission','userPartnership'])

Related

laravel eloquent problem with multiple when condition

I have this code
public function getUsers(){
$announcement = $this;
return User::query()
->when($announcement->users, function($query) use ($announcement) {
$query->whereIn('id', $announcement->users->pluck('id')->toArray());
})
->when($announcement->userTypes, function($query) use ($announcement) {
$query->whereIn('user_type_id', $announcement->userTypes->pluck('id')->toArray());
})
->when($announcement->organizations, function($query) use ($announcement) {
$query->whereIn('organization_id', $announcement->organizations->pluck('id')->toArray());
})
->when($announcement->organizations, function($query) use ($announcement) {
$query->whereHas('organization', function($q) use ($announcement) {
$q->where('organization_type_id', $announcement->organizationTypes->pluck('id')->toArray());
});
})
->get();
}
when I run this, what it returns is an empty array, however there is data that matches the given conditions and it is not because it does not return it to me

Laravel nested relation filter

Have a query, how I can filter results by translation relation (by name column)
$item = Cart::select('product_id','quantity')
->with(['product.translation:product_id,name','product.manufacturer:id,name'])
->where($cartWhere)
->get();
my model
Cart.php
public function product($language = null)
{
return $this->hasOne('App\Models\Product','id','product_id');
}
Product.php
public function translations()
{
return $this->hasMany('App\Models\ProductTranslation','product_id','id');
}
Update v1.0
do like this, but query takes too long time
$item = Cart::select('product_id','quantity')
->with(['product.translation', 'product.manufacturer:id,name'])
->where($cartWhere)
->when($search,function ($q) use ($search) {
$q->whereHas('product.translation', function (Builder $query) use ($search) {
$query->where('name', 'like', '%'.$search.'%');
$query->select('name');
});
}
)
->get() ;
Inside the array within your with() method, you can pass a function as a value.
Cart::select('product_id','quantity')
->with([
'product', function($query) {
$query->where($filteringAndConditionsHere);
}
]);
https://laravel.com/docs/7.x/eloquent-relationships#eager-loading

Why my filter request rewrite each other?

I want to do script where I can filter users. The problem is where I write one field, then another doesn't work. Always works only one last request, not all. How to change it?
if ($request->has('city'))
{
$user = User::with('user_data')->whereHas('user_data', function($query) use ($request) {
return $query->where('residence', $request->city);
})->get();
}
if ($request->has('age_from'))
{
$user = User::with('user_data')->whereHas('user_data', function($query) use ($request) {
return $query->where('date_of_birth', '<=', Carbon::now()->subYears($request->age_from));
})->get();
}
if ($request->has('age_to'))
{
$user = User::with('user_data')->whereHas('user_data', function($query) use ($request) {
return $query->where('date_of_birth', '>=', Carbon::now()->subYears($request->age_to));
})->get();
}
It seems you are not in the right way.Questions isn't enough to imagine your problem exactly.
But to fix it,tricks from below might help you.
You can use multiple whereHas like this.
$user = User::with('user_data')->whereHas('user_data', function($query) use ($request) {
return $query->where('residence', $request->city);
})
->whereHas('user_data', function($query) use ($request) {
return $query->where('date_of_birth', '<=', Carbon::now()->subYears($request->age_from));
})
->whereHas('user_data', function($query) use ($request) {
return $query->where('date_of_birth', '>=', Carbon::now()->subYears($request->age_to));
})->get();
If you want to filter it by $request, do like this.
$user = User::with('user_data');
if ($request->has('city')) {
$user->whereHas('user_data', function($query) use ($request) {
return $query->where('residence', $request->city);
});
}
if ($request->has('age_from')) {
$user->whereHas('user_data', function($query) use ($request) {
return $query->where('date_of_birth', '<=', Carbon::now()->subYears($request->age_from));
});
}
if($request->has('age_to')){
$user->whereHas('user_data', function($query) use ($request) {
return $query->where('date_of_birth', '>=', Carbon::now()->subYears($request->age_to));
});
}
$user = $user->get();
As a rule, all input field will present in the request when they're not disabled in your HTML, so it is not depending on the field value and your check for availability of the input variable using has() method always returns true for both the filled and blank inputs.
So you need to check for value using input() method.
Furthermore while it is possible to have more than one input value in a request, you need to add your criteria to the model before getting the results:
$user = User::with('user_data');
if ($request->input('city'))
{
$user->whereHas('user_data', function($query) use ($request) {
$query->where('residence', $request->city);
});
}
if ($request->input('age_from'))
{
$user->whereHas('user_data', function($query) use ($request) {
$query->where('date_of_birth', '<=', Carbon::now()->subYears($request->age_from));
});
}
if ($request->input('age_to'))
{
$user->whereHas('user_data', function($query) use ($request) {
$query->where('date_of_birth', '>=', Carbon::now()->subYears($request->age_to));
});
}
$user = $user->get();

How to use laravel collection methods

I'm trying to query a nested relation and getting the count of distant model count, I'm transforming this to a variable but it is not happening:
$companies = Company::where('is_client', '=', 1)
// load count on distant model
->with(['interactionSummaries.interaction' => function ($q) {
$q->withCount(['contactsAssociation' => function ($q) {
$q->whereHas('company', function ($q) {
$q->where('type', 'like', 'Research');
});
}]);
}])
->get()
->transform(function ($company) {
$company->contacts_association_count = $company->interactionSummaries
->pluck('interaction.contacts_association_count')
->collapse()
->sum();
});
When I try to dd($companies) or even return $companies I get null values placed in each array index
But when I do dd($company) inside transform
->transform(function ($company) {
$company->contacts_association_count = $company->interactionSummaries
->pluck('interaction.contacts_association_count')
->collapse()
->sum();
dd($company);
});
I get the single collection which states my query is running properly:
If I remove transform part and simply get the collection:
$companies = Company::where('is_client', '=', 1)
// load count on distant model
->with(['interactionSummaries.interaction' => function ($q) {
$q->withCount(['contactsAssociation' => function ($q) {
$q->whereHas('company', function ($q) {
$q->where('type', 'like', 'Research');
});
}]);
}])
->get()
I'm getting the desired output, But I don't know what it is happening after transform execution. In fact it is not plucking and adding the sum, as I'm getting all 0 in the counts.
I've marked them with red. Help me out in this
I think you are missing a return statement in the transform closure.
You need to put return $company statement in transform.

How to use laravel collection helper methods?

I'm trying to build a small application on laravel-5.4 where I'm having a relational query something like this:
$companies = Company::where('is_client', '=', 1)
// load count on distant model
->with(['interactionSummaries.interaction' => function ($q) {
$q->withCount(['contactsAssociation' => function ($q) {
$q->whereHas('company', function ($q) {
$q->where('type', 'like', 'Investor');
});
}]);
}])
->get();
Now I want to collect all the contact_association_counts generated from the query and add it to individual company collection For this I'm using pluck, collapse and sum method, but I don't know it is not calculating as desired. Following is the screenshots:
I get list of collection as following:
Now I get the attributes:
Now relational data:
Now interaction data, where the count belongs:
So for this I tried:
$companies = Company::where('is_client', '=', 1)
// load count on distant model
->with(['interactionSummaries.interaction' => function ($q) {
$q->withCount(['contactsAssociation' => function ($q) {
$q->whereHas('company', function ($q) {
$q->where('type', 'like', 'Investor');
});
}]);
}])
->get()
->transform(function ($company) {
$company->contacts_association_count = $company->interactionSummaries
->pluck('interaction.contacts_association_count')
->collapse()
->sum();
return $company;
});
But this is not calculating the counts, all the counts are coming to 0
Help me out in this. Thanks
I believe that your problem is the ->collapse() seeing your example after the ->pluck('interaction.contacts_association_count') you should have a flat array as [1,2,3,4,5] if you apply collapse() to a flat array it returns a void array [] and the sum of a void array is 0
$companies = Company::where('is_client', '=', 1)
// load count on distant model
->with(['interactionSummaries.interaction' => function ($q) {
$q->withCount(['contactsAssociation' => function ($q) {
$q->whereHas('company', function ($q) {
$q->where('type', 'like', 'Investor');
});
}]);
}])
->get()
->transform(function ($company) {
$company->contacts_association_count = $company->interactionSummaries
->pluck('interaction.contacts_association_count')
//->collapse()
->sum();
return $company;
});
I hope it works... good luck!

Categories