// if $id has value it will include "where('id','<',$id) else will return all" $wells = DB::table('well_s') ->when($id, function ($query, $id) { return $query->where('id','<',$id); }) ->paginate(20);