Laravel Query Builder Advance Where - laravel

Laravel Advance Query Builder not seeing parent method variable
public function read($status=null,$skip=0,$take=10,$orderby=array())
{
$table = DB::table('users')
->skip($skip)
->take($take)
->where(function($query) {
if($status)
$query->where('status','!=',$status);
});
}
This returns an error which $status variable undefined inside the advance where query. Is there anything to extend?

If you want to use variables in your closure, you must include them.
Change
->where(function($query) {
to
->where(function($query) use ($status) {

Related

In Laravel hasMany relationship How to use with and where?

In Job model :
public function jobApplications() {
return $this->hasMany(JobApplication::class, 'job_id');
}
In JobApplication Model
public function jobs()
{
return $this->belongsTo(Job::class, 'job_id');
}
In job_applications migration
$table->id();
$table->foreignId("job_id")->constrained("jobs");
$table->foreignId("user_id")->constrained("users");
$table->text('remarks')->nullable();
$table->unsignedInteger('status')->default(1);
I need to get all jobs and its job applications where job_applications.status = (user input status) and job_applications.user_id =authenticated users id. How can i get that?
Below is the syntax i tried, which returned undefined variable status
$jobs = Job::where('status',1);
$status =$request->status;
if($status){
$jobs = $jobs->whereHas('jobApplications', function($q){
$q->where('status',$status);
$q->where('user_id',Auth()->user()->id);
});
return $jobs->get();
Can any one sugest a solution?
If you use closer the variabl that is defined outside the closer is not accessible. so you should use it. Like this.
if($status){
$jobs = $jobs->whereHas('jobApplications', function($q) use($status){
$q->where('status',$status);
$q->where('user_id',Auth()->user()->id);
});
This will remove the error undefined veriable $status.
to use an external variable in closure method you need to pass that variable into method using "use" keyword like
if($status){
$jobs = $jobs->whereHas('jobApplications', function($q) use ($status) {
$q->where('status',$status);
$q->where('user_id',Auth()->user()->id);
});}
return $jobs->get();

how to use whereHas in laravel

I am new to laravel,
I need to create a query for the db,
$query = Deal::query();
I want to use the wherehas operator.
this is my code that is worked.
else if ($group_by == 'precedence') {
if($get_deals_for == 'noprecedence'){
$get_deals_for = 0;
}
$precedenceStatus = $get_deals_for;
$query-> where('precedence', '=', $precedenceStatus);
// \Log::info('The request precedence: '.$precedenceStatus);
}
I want to add this code also to the query
if($person) {
$query->whereHas('personnel', function ($subQuery) use ($person) {
$subQuery->where('id', '=', $person);
});
}
So I need to change the first code?
how I can convert the first code to wherehas?
the first code is from table called deal, the second section is from realtionship called personnel.
the second section worked in other places in the code, I just need to fix the first section and not understand what to write in the use
I try this and get error on the last }
else if ($group_by == 'precedence') {
if($get_deals_for == 'noprecedence'){
$get_deals_for = 0;
}
$precedenceStatus = $get_deals_for;
$query-> where('precedence', '=', $precedenceStatus)
-> when ($person, function($query) use($person) {
$query->whereHas('personnel', function ($query) use ($person) {
$query->where('id', '=', $person);
});
})
}
There is a method you can use called when(, so that you can have cleaner code. The first parameter if true will execute your conditional statement.
https://laravel.com/docs/9.x/queries#conditional-clauses
$result = $query
->where('precedence', '=', $precedenceStatus)
->when($person, function ($query) use ($person) {
$query->whereHas('personnel', fn ($q) => $q->where('id', '=', $person));
})
->get();
You should also be able to clean up your precedence code prior to that using when( to make the entire thing a bit cleaner.
Querying to DB is so easy in laravel you just need to what you want what query you want execute after that you just have to replace it with laravel helpers.Or you can write the raw query if you cant understand which function to use.
using,DB::raw('write your sql query').
Now Most of the time whereHad is used to filter the data of the particular model.
Prefer this link,[Laravel official doc for queries][1] like if you have 1 to m relation ship so u can retrive many object from one part or one part from many object.like i want to filter many comments done by a user,then i will right like this.
$comments = Comment::whereHas('user', function (Builder $query) {
$query->where('content', 'like', 'title%');
})->get();
$comments = Here will be the model which you want to retrive::whereHas('relationship name', function (Builder $query) {
$query->where('content', 'like', 'title%');
})->get();
you can also write whereHas inside whereHas.
[1]: https://laravel.com/docs/9.x/eloquent-relationships#querying-relationship-existence

How to write a query conditionally in laravel?

I am new to the laravel, i am joining three tables based on conditions every thing is working fine but i need to write conditionally if the $field is array it should run with whereIn otherwise it should run where condition,can you please help me to acheive this thing
//$field sometimes it's an array sometimes it's a string.
public function find($field){
}
For conditional constraints, you can use the when() clause:
$query = DB::table(...);
$data = $query
->when(is_array($field), function ($query) use ($field) {
$query->whereIn('my_field', $field);
}, function ($query) use ($field) {
$query->where('my_field', $field);
})
->get();
Now, as a tip, you could do this: Wrap the $fields variables to an array and then use always the whereIn clause. You can achieve this with the Arr::wrap() function:
use Illuminate\Support\Arr;
// ...
$query = DB::table(...);
$data = $query
->whereIn('my_field', Arr::wrap($field))
->get();
PS: I have linked the relevant functions to the docs so you can know how they work and their params.

Why my model can't see spatie permissions trait method on relationship subquery?

I use spatie/laravel-permissions composer package in my laravel projects.
When I run this query:
$jobs = Job::whereIn('id', $ids)->whereHas('user', function ($query) {
$query->hasRole('company');
})->get();
Return error message
Call to undefined method
Illuminate\Database\Eloquent\Builder::hasRole()
How I can fix my problem in my case?
The hasRole-method is not a scope and can't be used on a Builder instance.
I think you should be able to use the role-scope in your application.
$jobs = Job::whereIn('id', $ids)->whereHas('user', function ($q) {
return $q->role('company');
})->get();
Because $user passed to the function closure is a query builder instance and not an instance of the User model so where you're declaring the $user above, make sure to get an instance
$user = User::where(......, ........)->first(); // Without first() it's a query builder
$jobs = Job::whereIn('id', $ids)->whereHas('user', function ($user) {
$user->hasRole('company');
})->get();

Need to include a variable in a function of a query in Laravel

I have something like this:
$configurations = Configs:all();
foreach($configs as $config){
$documents = document::whereHas('user', function ($q) {
$q->where("portal_id", $config->id),})->get()
...
}
This does not work because $config is not defined in the query function.
How do I put the $config->id object there?
A closure encapsulates its scope, meaning that it has no access to the scope in which it is defined or executed. It is, however, possible to inherit variables from the parent scope (where the closure is defined) into the closure with the use keyword:
$documents = document::whereHas('user', function ($q) use ($config) {
$q->where("portal_id", $config->id),})->get()
}
You can extend the scope of variable with use()
$documents = document::whereHas('user', function ($q) use($config->id) {
$q->where("portal_id", $config->id),})->get()
...
}
also you can write like this, no need to write foreach loop:
$configurations = Configs:all();
$documents = Document::whereHas('user', function($q) use($configurations) {
$q->whereIn('portal_id', $configurations->pluck('id'));
});

Resources