Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR PHP

laravel eloquent where if a variable has value

//by this we can create filter with "And" condition when value exists

$query->when($what,function($q,$what){
  $q->where('log_event_description', 'like', '%' . $what . '%');
})->when($detail,function($q,$detail){
  $q->where('log_details', 'like', '%' . $detail . '%');
})->when($who,function($q,$who){
  $q->where('log_event_type', 'like', '%' . $who . '%');
})->when($when,function($q,$when){
  $q->where('created_at', 'like', '%' . $when . '%');
});
 
PREVIOUS NEXT
Tagged: #laravel #eloquent #variable
ADD COMMENT
Topic
Name
4+7 =