Why do I get an Undefined Variable: request in laravel - laravel

I got an error with my query, it seems that my Request $request variable can't be used inside the function($quer){$query->} below is my sample code.
Table1::addSelect(['PaymentStatus' => Table2::selectRaw('COUNT(*)')
->whereColumn('AccountNumber','Table1.AccountNumber')
->whereColumn('ServicePeriodEnd','Table1.ServicePeriodEnd')
])->whereNotExists(function($query){
$query->select(DB::raw('*'))
->from('Table2')
->whereRaw('Table1.AccountNumber = Table2.AccountNumber')
->whereRaw('Table1.ServicePeriodEnd = Table2.ServicePeriodEnd')
->where('AccountNumber', $request->AccountNum); // this part gives an Undefined Variable: request
})->where('AccountNumber', $request->AccountNum)
->orderBy('ServicePeriodEnd','desc')
->get();

The $request variable can not be read from the function due to it's not been declared to be used in the function. Replace this line:
])->whereNotExists(function($query){
With this line
])->whereNotExists(function($query) use ($request) {
This is relatively new syntax btw. Example can be found at https://www.php.net/manual/en/functions.anonymous.php

Use request use ($request) in the callback function query
->whereNotExists(function($query) use ($request) {
Table1::addSelect(['PaymentStatus' => Table2::selectRaw('COUNT(*)')
->whereColumn('AccountNumber','Table1.AccountNumber')
->whereColumn('ServicePeriodEnd','Table1.ServicePeriodEnd')
])->whereNotExists(function($query) use ($request) {
$query->select(DB::raw('*'))
->from('Table2')
->whereRaw('Table1.AccountNumber = Table2.AccountNumber')
->whereRaw('Table1.ServicePeriodEnd = Table2.ServicePeriodEnd')
->where('AccountNumber', $request->AccountNum); // this part gives an Undefined Variable: request
})->where('AccountNumber', $request->AccountNum)
->orderBy('ServicePeriodEnd','desc')
->get();

Related

Laravel Query Relationship Constraints - Inject Variable

this should be an easy question. I am trying to query my Post model to find all posts that have a certain topic. Below is the controller method.
// Get collection of posts with this topic
// Get specific topic for this page
$postTopic = Topic::where('topic_name', $topic)->first();
$posts = Post::whereHas('topic', function (Builder $query) {
$query->where('topic_id', $postTopic->id);
})->get()->sortByDesc('created_at');
I am struggling with how to define the $postTopic variable. Thank you!
Your initial approach is close, but the tricky thing about anonymous functions is "scope", where variables, unless explicitly passed into the scope, are not available. For example:
$postTopic = Topic::where('topic_name', $topic)->first();
$posts = Post::whereHas('topic', function (Builder $query) {
$query->where('topic_id', $postTopic->id);
})->get()->sortByDesc('created_at');
While this code looks correct, since you're not passing $postTopic into function (Builder $query), you'll receive the error:
Undefined variable `$postTopic` ...
To resolve this, you need to use a use() clause:
$postTopic = Topic::where('topic_name', $topic)->first();
$posts = Post::whereHas('topic', function (Builder $query) use ($postTopic) {
$query->where('topic_id', $postTopic->id);
})->get()->sortByDesc('created_at');
Any number of variables can be passed and referenced in scope using use(), and you should no longer get the undefined variable error.

How to use OR and AND with where clause in laravel 5.3?

-- Query I want to use :
select * from users where (email like '%email#email.com%' OR username like 'myusername') AND (password like '%1234#abcde%')
-- My laravel code for above query is :
$getInfo = Users::where('password','like','%'.$request->password.'%')
->where(function($query){
$query->where('email','like','%'.$request->username.'%')
->orWhere('username','like','%'.$req->username.'%');
})->get();
Here I am getting an error Undefined variable: request
You need to pass $request to your closure function like use ($request)
$getInfo = Users::where('password','like','%'.$request->password.'%')
->where(function($query) use ($request){
$query->where('email','like','%'.$request->username.'%')
->orWhere('username','like','%'.$req->username.'%');
})->get();
Also its not a good idea to search user details using like with wildcard (%) on both sides, it should be just like without wildcards or just use equality comparison =
$getInfo = Users::where('password','like','%'.$request->password.'%')
->where(function($query) use ($request){
$query->where('email','like','%'.$request->username.'%')
->orWhere('username','like','%'.$request->username.'%');
})->get();
You have can variables to closure using use ($request).
You have also a typo
change
->orWhere('username','like','%'.$req->username.'%');
to
->orWhere('username','like','%'.$request->username.'%');

Laravel 5: How to pass parameters to inner where query

I've following query:
$visible = array(1,2,3);
$views = DB::table('ims_view')
->Where(function($query) {
$query->where('iv_status', 1)->whereIn('id', $visible);
})->orderBy('iv_name', 'asc')->get();
Error: Undefined variable: visible
How to pass the variabnle to inner where?
you have to use the variable you want passed into the scope like so:
use ($visible)
`$views = DB::table('ims_view')
->Where(function($query) use ($visible){
$query->where('iv_status', 1)
->whereIn('id', $visible);
})->orderBy('iv_name', 'asc')->get();`

Call to undefined method Illuminate\Database\Query\Builder::when()

I'm getting the error of undefined when() method in laravel 5 when use Database: Query Builder Conditional Statements
https://laravel.com/docs/5.2/queries#conditional-statements
Call to undefined method Illuminate\Database\Query\Builder::when()
$job = $request->input('Job');
$EmpDetails = DB::table('EMPLOYEES')->when($job, function ($query) {
return $query->where('Job', '=', $job);
})->get();
print_r($EmpDetails);
$EmpDetails = DB::table('EMPLOYEES')->where('JOB',$job)->get();
I think this should work.
Edit: You might want to use advance where. In that case:
$EmpDetails = DB::table('EMPLOYEES')->where(function($query) use ($job)
{
$query->where('JOB',$job);
})->get()

Laravel Query Builder Advance Where

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) {

Resources