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()
Related
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();
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();
$modules = Role::with(['rights' => function ($q) {
return $q->with('module');
}])->where('id', $user->role_id)->get();
Can someone help with this condition not working with the where statement?
You can not return inside the relation inside the with() closure.
Instead, you can use Nested Eager Loading for this.
$modules = Role::where('id', $user->role_id)
->with('rights.module')
->get();
This is my query builder. But an error is shown
ERROR => count(): Parameter must be an array or an object that implements
Countable
Route::get('project', function () {
$project = DB::table('pams_project')
->join('pams_developer', 'pams_project.dev_id', '=', 'pams_developer.id')
->select(array('pams_developer.developer_name',DB::raw(count('pams_project'.'dev_id'))))
->get();
return view('projectByDeveloper', ['project' => $project]);
});
can anyone figure out my problems ?
use DB::raw("count(pams_project.dev_id) as count")
$project = DB::table('pams_project')
->join('pams_developer', 'pams_project.dev_id', '=', 'pams_developer.id')
->select(array('pams_developer.developer_name',DB::raw("count(pams_project.dev_id) as count")))
->get();
But also make sure to use GroupBy() with this.
If you want to get the count of collection u can simply use $project->count()
Hope this helps
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) {