Laravel relationship data with different tables nased on conditions - php

i am working on a snippet but i have to check another table if that customer data exists return the data if the customer data does not exist on the employment table also return the data based on the former check then ignore the rest but if it does i want to use the condition specified to check and return that data. Here's what i have tried.
Customers::query()
->distinct('id')
->whereHas('quote')
->where(
function ($query) {
$query->whereHas('profile', function ($query) {
$query->where(function ($query) {
$query->whereNull('nin')->WhereNull('bvn');
})->orWhere(function ($query) {
$query->whereNotNull('nin')->whereNotNull('bvn');
});
})->orWhereHas('employment', function ($query) {
$query->where(function ($query) {
$query->whereNull('bank_statement_url')->WhereNull('mono_account_statement_url');
})->orWhere(function ($query) {
$query->whereNotNull('bank_statement_url')->whereNotNull('mono_account_statement_url');
});
});
}
)->paginate(30);

I cleaned up your code a bit as well as changing your orWhereHas() to whereHas():
Customers::query()
->distinct('id')
->whereHas('quote')
->whereHas('profile', function ($query) {
$query->where(function ($query) {
$query->whereNull('nin')->WhereNull('bvn');
})->orWhere(function ($query) {
$query->whereNotNull('nin')->whereNotNull('bvn');
});
})->whereHas('employment', function ($query) {
$query->where(function ($query) {
$query->whereNull('bank_statement_url')
->whereNull('mono_account_statement_url');
})->orWhere(function ($query) {
$query->whereNotNull('bank_statement_url')
->whereNotNull('mono_account_statement_url');
});
}
)->paginate(30);

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 (8.x) Is there a better Eloquent query for this many-to-many filtering problem?

What I have
movies many-to-many stars
movies many-to-many tags
studio one-to-many movies
I pass studio name, star name and/or tag name as optional query strings
What I need
Select all movies with tag and star and studio if all 3 are passed as params
Select all movies with tag and star if those 2 are passed as params
Select all movies with star and studio if those 2 are passed as params
Select all movies with tag and studio if those 2 are passed as params
Select all movies with tag or star or studio if any one is passed as param
Basically, Simplify this query
My Possibly Stuipd code
public function index(Request $request)
{
$movies = Movie::with('stars')->with('tags')->with('studio');
// all matches
// if all params are present
if ($request->star && $request->tag && $request->studio)
$movies->whereHas('stars', function ($query) use ($request) {
$query->where('name', $request->star);
})->whereHas('tags', function ($query) use ($request) {
$query->where('name', $request->tag);
})->whereHas('studio', function ($query) use ($request) {
$query->where('name', $request->studio);
});
// star and tag with no studio
if ($request->star && $request->tag && !$request->studio)
$movies->whereHas('stars', function ($query) use ($request) {
$query->where('name', $request->star);
})->whereHas('tags', function ($query) use ($request) {
$query->where('name', $request->tag);
});
// star and studio with no tag
if ($request->star && !$request->tag && $request->studio)
$movies->whereHas('stars', function ($query) use ($request) {
$query->where('name', $request->star);
})->whereHas('studio', function ($query) use ($request) {
$query->where('name', $request->studio);
});
// studio and tag with no star
if (!$request->star && $request->tag && $request->studio)
$movies->whereHas('tags', function ($query) use ($request) {
$query->where('name', $request->tag);
})->whereHas('studio', function ($query) use ($request) {
$query->where('name', $request->studio);
});
// any one matches
if ($request->star || $request->tag || $request->studio)
$movies->whereHas('stars', function ($query) use ($request) {
$query->where('name', $request->star);
})->orWhereHas('tags', function ($query) use ($request) {
$query->where('name', $request->tag);
})->orWhereHas('studio', function ($query) use ($request) {
$query->where('name', $request->studio);
});
return response($movies->paginate(20));
}
I think what you need is this:
public function index(Request $request)
{
$movies = Movie::with('stars')->with('tags')->with('studio');
// all matches
// tag
if ($request->tag)
$movies->whereHas('tags', function ($query) use ($request) {
$query->where('name', $request->tag);
});
// star
if ($request->star)
$movies->whereHas('stars', function ($query) use ($request) {
$query->where('name', $request->star);
});
// studio
if ($request->studio)
$movies->whereHas('studio', function ($query) use ($request) {
$query->where('name', $request->studio);
});
return response($movies->paginate(20));
}
This way you only filter your relation if its key is provided.

how can i do query biulder with (AND) for 3 or more parameters

im did a query for brand of car and the model but when i did added a
query to price range (whereBetwen) its not working
public function indexBrowse(Request $request)
{
$brand=$request->get('searchbrand');
$model=$request->get('searchmodel');
$searchtype=$request->get('searchtype');
$from=$request->get('pricefrom');
$to=$request->get('priecto');
$cars=car::where('brand', 'like', '%'.$brand.'%')
->where(function ($query) use ($model) {
$query->where('model','like','%'.$model.'%');
})->where(function ($query2) use ($from,$to) {
$query2->car::whereBetween('price',['%'.$from.'%','%'.$to.'%']);
})->get();
dd($cars);
$success = 'Results Car';
return view('cars.index', compact('cars', 'success'));
}
Something like this if you want to perform all these where with and condition.
car::where('brand', 'like', '%'.$brand.'%')
->where('model','like','%'.$model.'%')
->whereBetween('price'['%'.$from.'%','%'.$to.'%'])->get();

Laravel Filter using various relations

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'])

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

Categories