0

This is the ajax request in my controller :

public function dtajax()
    {
         $users = User::all();

          $users =  DB::table('tc_users')
            ->join('users_tc_users','users_tc_users.tc_users_id', '=','tc_users.id')
            ->join('users','users.id', '=','users_tc_users.users_id') 
            ->leftJoin('cars','cars.users_id', '=','users.id')
            ->groupBy('users.id')
            ->select(DB::raw("users.id as id ,  users.name as name , users.email as email ,  users.Expiration_Date as Expiration_Date , users.phone_number as phone_number ,users.allowedcar as allowedcar ,COUNT(cars.id) as cars_count  "))
            ->get(array("users.id as id ,  users.name as name , users.email as email ,  users.Expiration_Date as Expiration_Date , users.phone_number as phone_number ,users.allowedcar as allowedcar,COUNT(cars.id) as cars_count "));
foreach ($users as $user){
    echo $user->cars_count;
}
        $values = DB::table('users')->select($this->listing_cols)->whereNull('deleted_at');
        $out = Datatables::of($values)->make();
        $data = $out->getData();

        $fields_popup = ModuleFields::getModuleFields('Users');

        for($i=0; $i < count($data->data); $i++) {
            for ($j=0; $j < count($this->listing_cols); $j++) { 
                $col = $this->listing_cols[$j];
                //  $this->listing_cols->kk;
                                // echo $col['no_of_cars'];

                if($fields_popup[$col] != null && starts_with($fields_popup[$col]->popup_vals, "@")) {
                    $data->data[$i][$j] = ModuleFields::getFieldValue($fields_popup[$col], $users, $data->data[$i][$j]);
                    // $data->data[$i][$j] .=  'yssss';
                }
                if($col == $this->view_col) {

                    $data->data[$i][$j] = '<a href="'.url(config('laraadmin.adminRoute') . '/users/'.$data->data[$i][0]).'">'.$data->data[$i][$j].'</a>';
                }
                // else if($col == "author") {
                //    $data->data[$i][$j];
                // }
            }


            if($this->show_action) {
                $output = '';
                if(Module::hasAccess("Users", "edit")) {
                    $output .= '<a href="'.url(config('laraadmin.adminRoute') . '/users/'.$data->data[$i][0].'/edit').'" class="btn btn-warning btn-xs" style="display:inline;padding:2px 5px 3px 5px;"><i class="fa fa-edit"></i></a>';
                }

                if(Module::hasAccess("Users", "delete")) {
                    $output .= Form::open(['route' => [config('laraadmin.adminRoute') . '.users.destroy', $data->data[$i][0]], 'method' => 'delete', 'style'=>'display:inline']);
                    $output .= ' <button class="btn btn-danger btn-xs" type="submit"><i class="fa fa-times"></i></button>';
                    $output .= Form::close();
                }

                $data->data[$i][] = (string)$output;
            }

        }
        $out->setData($data);

        return $out;
    }

and thats the ajax function in the front :

$(function () {
    $("#example1").DataTable({
        processing: true,
        serverSide: true,
        ajax: "{{ url(config('laraadmin.adminRoute') . '/user_dt_ajax') }}",
        language: {
            lengthMenu: "_MENU_",
            search: "_INPUT_",
            searchPlaceholder: "Search"
        },
        @if($show_actions)
        columnDefs: [ { orderable: false, targets: [-1] }],
        @endif
    });
    $("#user-add-form").validate({

    });
});
</script>

What I want simply is to add new column called cars_count and insert its value from the select query above I can echo it in by

 foreach ($users as $user){
    echo $user->cars_count;
}     

I can't fill the table from the view because it's filled from the ajax request by the basic data

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
  • Can you be a bit more specific? What routes are you using and how are they set up? What is your expected result and what do you get now? Usually working with datatables you use 2 routes(1 to draw page, 1 to fill datatable) or repurpose 1 route into 2 paths using the `Illuminate\Http\Request` class's `ajax()` function which returns true for the ajax call, so you can create this second path in your code. + datatables requires json encoded data. Just echo the `$users` collection, and the data should be ready to go, assuming you took care of your columndefinition(which also misses from your info). – Techno Apr 23 '19 at 19:17
  • the dtajax function from the controller is drawing the table i want to add column and it's vlaue throw it – zandroid arabapps Apr 24 '19 at 09:40

0 Answers0