My code get data from database using AJAX the problem is i want to filter data by month year and quarter and the code that does that is ->where(\DB::raw('MONTH(created_at)'), Carbon::today()->month); but i can't put it in a variable for example and call it so i did this it works but looks ugly Hello I have this Code in Dashboard Controller: public function readData(){ $TableB1 = \DB::table('users') ->join('group_user', 'users.id', '=', 'group_user.user_id') ->join('groups', 'groups.id', '=', 'group_user.group_id') ->select( 'users.name as name', 'group_user.user_id as id', 'groups.name as groupname' ) ->get(); $filter = $_REQUEST["filter"]; // Get the variable data through AJAX call from the index foreach ($TableB1 as &$entry){ if ($filter == 'month'){ // Filter data by month $meetings = \DB::table('meetings') ->where('company_id', 1)->where('owned_by_id', $entry->id) ->where(\DB::raw('MONTH(created_at)'), Carbon::today()->month); } else if ($filter == 'quarter'){ // Filter data by quarter $meetings = \DB::table('meetings') ->where('company_id', 1)->where('owned_by_id', $entry->id) ->where(\DB::raw('QUARTER(created_at)'), Carbon::today()->quarter); } else if ($filter == 'year'){ // Filter data by year $meetings = \DB::table('meetings') ->where('company_id', 1)->where('owned_by_id', $entry->id) ->where(\DB::raw('YEAR(created_at)'), Carbon::today()->year); } else { // Filter data by day $meetings = \DB::table('meetings') ->where('company_id', 1)->where('owned_by_id', $entry->id) ->where(\DB::raw('DAY(created_at)'), Carbon::today()->day); } $entry->meetingsCount = $meetings->count(); } return $TableB1; } And in index.blade I have this code to retrieve the data using ajax var filter = 'day'; $(document).ready(function(){ $.get('/dashboard/read?filter=' + filter, function(data){ $.each(data,function(i,value){ var tr =$("<tr/>"); tr.append($("<th/>",{ text : value.groupname })).append($("<th/>",{ text : value.name })).append($("<th/>",{ text : value.meetingsCount })).append($("<th/>",{ text : value.callsCount })).append($("<th/>",{ text : value.leadsCount })) $('#tableData').append(tr); }) }) $("#day").click(function() { // Filter data by day filter = 'day'; $('#tableData').find('tr').empty(); // Clear Table 1st $.get('/dashboard/read?filter=' + filter, function(data){ $.each(data,function(i,value){ var tr =$("<tr/>"); tr.append($("<th/>",{ text : value.groupname })).append($("<th/>",{ text : value.name })).append($("<th/>",{ text : value.meetingsCount })).append($("<th/>",{ text : value.callsCount })).append($("<th/>",{ text : value.leadsCount })) $('#tableData').append(tr); }) }) }); $("#month").click(function() { //Filter data by month $('#tableData').find('tr').empty(); // Clear Table 1st filter = 'month'; $.get('/dashboard/read?filter=' + filter, function(data){ $.each(data,function(i,value){ var tr =$("<tr/>"); tr.append($("<th/>",{ text : value.groupname })).append($("<th/>",{ text : value.name })).append($("<th/>",{ text : value.meetingsCount })).append($("<th/>",{ text : value.callsCount })).append($("<th/>",{ text : value.leadsCount })) $('#tableData').append(tr); }) }) }); $("#quarter").click(function() { //Filter data by quarter filter = 'quarter'; $('#tableData').find('tr').empty(); // Clear Table 1st $.get('/dashboard/read?filter=' + filter, function(data){ $.each(data,function(i,value){ var tr =$("<tr/>"); tr.append($("<th/>",{ text : value.groupname })).append($("<th/>",{ text : value.name })).append($("<th/>",{ text : value.meetingsCount })).append($("<th/>",{ text : value.callsCount })).append($("<th/>",{ text : value.leadsCount })) $('#tableData').append(tr); }) }) }); $("#year").click(function() { //Filter data by year filter = 'year'; $('#tableData').find('tr').empty(); // Clear Table 1st $.get('/dashboard/read?filter=' + filter, function(data){ $.each(data,function(i,value){ var tr =$("<tr/>"); tr.append($("<th/>",{ text : value.groupname })).append($("<th/>",{ text : value.name })).append($("<th/>",{ text : value.meetingsCount })).append($("<th/>",{ text : value.callsCount })).append($("<th/>",{ text : value.leadsCount })) $('#tableData').append(tr); }) }) }); I know its a mess but how to refactor this code to make it look a little bit nicer the code works but looks awful