Laravel when clause is throwing undefined variable - laravel

I am trying to make a conditional where, inside a select statement. It is throwing undefined variable 'class_id'. What is going wrong here. The variable class_id got some value but is throwing 'Undefined variable: class_id'.
$class_id = isset($searchData[1]) ? $searchData[1] : '';
\Log::info($class_id);
$students = \User::where('role', 'student')
->where('fullName', 'like', '%'.$student_name.'%')
->orWhere('username', 'like', '%'.$student_name.'%')
->orWhere('email', 'like','%'.$student_name.'%')
->when($class_id != '',
function ($q) {
return $q->where('studentClass', '=', $class_id);
})
->get();

You are using the wrong syntax for when(). You are using it in the perfect scenario, and you can omit initializing the variable above.
Laravel 5.2 -> 5.5
$class_id = isset($searchData[1]) ? $searchData[1] : '';
$students = \User::where('role','student')
->where('fullName','like','%'.$student_name.'%')
->orWhere('username','like','%'.$student_name.'%')
->orWhere('email','like','%'.$student_name.'%')
->when($class_id, function ($query) use ($class_id) {
return $query->where('studentClass','=', $class_id);
})->get();
Laravel 5.6+
$students = \User::where('role','student')
->where('fullName','like','%'.$student_name.'%')
->orWhere('username','like','%'.$student_name.'%')
->orWhere('email','like','%'.$student_name.'%')
->when($searchData[1], function ($query, $class_id) {
return $query->where('studentClass','=', $class_id);
})->get();

Related

Laravel 8 - How to use where conditions for relation's column

hello friends can you help me to get the contents of $filter? I want to run where which is where the target column is in the relation array
$filter = $request->get('Biogear');
$data = DetailBarang::with(['barang' => function ($q) use ($filter) {
$q->where('brand', '=', $filter);
}])->get();
return response()->json($data);
you can try this
$filter = $request->get('Biogear');
$data = DetailBarang::with('barang')->whereHas('barang',function (Illuminate\Database\Eloquent\Builder $q) use ($filter) {
$q->where('brand', '=', $filter);
})->get();
return response()->json($data);

Laravel apply pagination based on condition in query

I am using laravel eloquent for db operation. I create a done function in that I want to apply my pagination if the variable has value if the variable value is not found then it should use laravel get() method. Here is my code
$query = DB::table('mytable')
->when($searchBy != null, function ($query) use ($searchBy) {
return $query->where('mytable.id', '=', $searchBy)
->orWhere('orders.id', '=', $searchBy)
})
->orderBy('mytable.id', 'desc')
->paginate($recordsPerPage, ['*'], 'page', $pageNub);
I want to add pagination if $recordsPerPage variable is > 0, otherwise it should be use get() method
You would assign the builder to a variable and use some type of conditional here:
$query = DB::table('mytable')
->when($searchBy, function ($query, $searchBy) {
return $query->where('mytable.id', '=', $searchBy)
->orWhere('orders.id', '=', $searchBy)
})->orderBy('mytable.id', 'desc');
$result = ($recordsPerPage > 0) ?
$query->paginate(...) :
$query->get();

WhereHas not working, in Has Many Relationsip

Im try to using where to show data is_default=1 in relationship using whereHas like this code
$data = $this->user->with('profile', 'account')
->whereHas('account', function ($query) use ($id) {
return $query->where('is_default', 1);
})
->where('id', $id)
->first();
but my return still showing all data of account, even i set where is_default=1
how i can solved this problem, please help me
This should work.
$data = $this->user->with('profile')->where('id', $id)
->whereHas('account', function($q) use ($id){
return $q->where('is_default', 1);
})->first();
I've been solved this problem using this code
$data = $this->user->with(['profile', 'account_default' => function ($q) use ($id) {
$q->where('is_default', 1);
}])
->where('id', $id)
->first();

Eloquent where condition AND ( condition OR condition OR condition )

Can somebody tell me what am I doing wrong?
I want to get all projects with status 0 and if any of data matches in eloquent.
My code is like this
public function search(Request $request){
if($request->has('q')){
$search_query = $request->input('q');
$projects = Project::where(['status' => '0'])->get();
$projects = $projects->where(function($query) use ($search_query) {
$query->where('title','LIKE','%'.$search_query.'%')
->orWhere('shortDescription','LIKE','%'.$search_query.'%')
->orWhere('longDescription','LIKE','%'.$search_query.'%')
->orWhere('tags','LIKE','%'.$search_query.'%')
->orWhere('projectLink','LIKE','%'.$search_query.'%');
});
$projects->get();
dd($projects);
}
}
And I am getting this error
ErrorException
explode() expects parameter 2 to be string, object given
I solved the problem but changed the code a little bit... If you like, take it! I tested and it's working.
if($request->has('q')){
// Your $request->q Value
$term = $request->q;
$projects = DB::table('tbl_projects')->where('status', '=', 0)
->where(function ($query) use ($term) {
$query->where('title', 'LIKE', '%'.$term.'%')
->orWhere('shortDescription', 'LIKE', '%'.$term.'%')
->orWhere('longDescription', 'LIKE', '%'.$term.'%')
->orWhere('tags', 'LIKE', '%'.$term.'%')
->orWhere('projectLink', 'LIKE', '%'.$term.'%');
})
->get();
//dd($projects);
}
Raw query is:
select * from `tbl_projects` where `status` = 0 and (`title` LIKE %bla bla% or `shortDescription` LIKE %bla bla% or `longDescription` LIKE %bla bla% or `tags` LIKE %bla bla% or `projectLink` LIKE %bla bla%))
Regards!
Your error is
$projects = Project::where(['status' => '0'])->get();
(['status' => '0']) Where clause params required is different. You must use ('status', '0') or ('status', '=', '0')
When you use ->get(), you have a Collection or EloquentCollection object. You need a QueryBuilder
Try to implement a better programation logic.
A better implementation is
public function search(Request $request){
if($request->has('q')){
// Add array with field coincidences target. Is better if this array is a const static field in the Project model
$searchFields = ['title', 'shortDescription', 'longDescription', 'tags', 'projectLink' ];
// Now you can add 100 fields for search coincidences easily
$search_query = $request->input('q');
$projects = Project::where('status', '0');
foreach($searchFields as $searchField){
if ($searchField == 'title'){
$projects = $projects->where($searchField,'LIKE',"%$search_query%");
continue;
}
$projects = $projects->orWhere($searchField,'LIKE',"%$search_query%");
}
$projects = $projects->get();
dd($projects);
}
}

join query result in "Call to a member function leftJoin() on null"

I am trying to run a join query from my controller on laravel but i am getting "Call to a member function leftJoin() on null"
public function tracking_groupindividual(Request $request)
{
$user_id = $request->user_id;
$group_id = $request->group_id;
$get_group_individual_details = DB::table('group_usermapping as gu')
->select(
'um.user_id',
'um.user_name',
'um.image_path',
'um.mobile_number',
'ul.latitude',
'ul.longitude',
'um.isnearby_on',
'gu.isadmin',
'um.profile_status',
'ul.timedout'
)
->join('user_master as um','gu.user_id','=','um.user_id')
->join('groupusers_location as ul','ul.user_id','=','um.user_id','ul.group_id','=','gu.group_id')
->where(function ($query) use ($group_id) {
$query->where('gu.group_id', '=', $group_id)
->where('gu.isactive','=','1')->where('um.isactive','=','1');
})->get();
return $get_group_individual_details;
}
I found a problem in the second join and fixed it
public function tracking_groupindividual(Request $request)
{
$user_id = $request->user_id;
$group_id = $request->group_id;
$get_group_individual_details = DB::table('group_usermapping as gu')
->select(
'um.user_id',
'um.user_name',
'um.image_path',
'um.mobile_number',
'ul.latitude',
'ul.longitude',
'um.isnearby_on',
'gu.isadmin',
'um.profile_status',
'ul.timedout'
)
->join('user_master as um','gu.user_id','=','um.user_id')
->join('groupusers_location as ul', function ($join) {
$join->on('ul.user_id','=','um.user_id')
->where('ul.group_id','=','gu.group_id');
})
->where(function ($query) use ($group_id) {
$query->where('gu.group_id', '=', $group_id)
->where('gu.isactive','=','1')->where('um.isactive','=','1');
})->get();
return $get_group_individual_details;
}
try to use DB::raw() with aliases!!!
$get_group_individual_details = DB::table(DB::raw('group_usermapping as gu'))
->join(DB::raw('user_master as um'),DB::raw('gu.user_id'),'=',DB::raw('um.user_id'))
->join(DB::raw('groupusers_location as ul'),DB::raw('ul.user_id'),'=',DB::raw('um.user_id'),DB::raw('ul.group_id'),'=',DB::raw('gu.group_id'))
->select(DB::raw('um.user_id'),DB::raw('um.user_name'),DB::raw('um.image_path'),DB::raw('um.mobile_number'),DB::raw('ul.latitude'),DB::raw('ul.longitude'),DB::raw('um.isnearby_on'),DB::raw('gu.isadmin'),DB::raw('um.profile_status'),DB::raw('ul.timedout'))
->where(function ($query) use ($group_id) {
$query->where(DB::raw('gu.group_id'), '=', $group_id)->where(DB::raw('gu.isactive'),'=','1')->where(DB::raw('um.isactive'),'=','1');
})->get();
return $get_group_individual_details;
and replace use Illuminate\Support\Facades\DB; with use DB;

Resources