我想在用户控制器中循环抛出此自定义查询

时间:2019-04-24 13:34:46

标签: laravel-5 laraadmin

我想更改dtajax函数以循环将自定义查询不放入用户表中。

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("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 ");

    // print_r($users);
    $values = DB::table('users')->select($this->listing_cols)->whereNull('deleted_at');
    $out = Datatables::of($values)->make(false);
    // print_r($out);
    $datax = $out->getData();
    print_r($datax);
    // $datax->data =  Datatables::of($users)->make()->getData();
    // $data = $datax->data;

    // for ($i = 0; $i < count($datax->data); $i++) {
    //  foreach ($users as $user) {
    //      // echo $user->cars_count;
    //      //   print_r($user);
    //      array_push($datax->data[$i], $user->cars_count);
    //  }
    // }
    // print_r($data);
    $temp_cols = array_push($this->listing_cols, 'ttt');
    //  print_r($temp_cols);

    $fields_popup = ModuleFields::getModuleFields('Users');
    $temp_field = array_push($fields_popup, 'ttt');
    for ($i = 0; $i < count($data); $i++) {
        for ($j = 0; $j < count($temp_cols); $j++) {
            $col = $temp_cols[$j];
            //  $this->listing_cols->kk;
            // echo $col['no_of_cars'];

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

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


        if ($this->show_action) {
            $output = '';
            if (Module::hasAccess("Users", "edit")) {
                // print_r($datax->data[$i]);
                $output .= '<a href="' . url(config('laraadmin.adminRoute') . '/users/' . $datax->data[$i]->id . '/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', $datax->data[$i]->id], '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();
            }

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

    return $out;
}

当我从查询中删除get()时,当我尝试在$ users查询上使用get数据时,像$ datax = $ users-> getData();时,出现列未找到错误。  我得到那个错误:在数组上调用成员函数getData() (这是一个laraadmin控制器)

0 个答案:

没有答案