JsGrid and laravel

1.7k Views Asked by At

I'm new to JsGrid and i am trying to make some functions like https://www.webslesson.info/2018/08/live-table-add-edit-delete-using-php-with-jsgrid-plugin.html in laravel. But i got an error that says Undefined index: id. I am trying to make call data from database with json and populate it in the jsgrid but unsuccessful. Sorry for being a noob. please correct me if im wrong.

Here is my jsgrid.js

$("#validation").jsGrid({
width: "100%",
filtering: true,
editing: true,
inserting: true,
sorting: true,
paging: true,
autoload: true,
pageSize: 15,
pageButtonCount: 5,
autosearch: true,
deleteConfirm: "Do you really want to delete the client?",
controller: {
  loadData: function(filter){
      return $.ajax({
          type: "GET",
          url: "../../../api/users",
          data: filter,
          dataType: "JSON"
      });
  }  
},
fields: [
    { 
        name: "Id", 
        type: "text", 
        width: 30
    },
    { 
        name: "Name", 
        type: "text", 
        width: 150, 
    },
    { 
        name: "Email", 
        type: "text", 
        width: 150
    },
    { 
        name: "Password", 
        type: "text", 
        width: 150, 
    },
    { 
        name: "Created at", 
        type: "text", 
        width: 150
    },
    { 
        name: "Married", 
        type: "checkbox", 
        title: "", 
        width: 30,
        sorting: false 
    },
    { 
        type: "control" 
    }
]

});

and here is my controller

public function ap_index(User $users)
{
    $connect = mysqli_connect("localhost", "root", "", "DCAPT");
    $method = $_SERVER['REQUEST_METHOD'];
    if($method == 'GET')
    {
        $data = array (
            ':Id'   => "%"  .   $_GET['id'] . "%", <------- //The error is here Undefined Index: id
            ':Name'   => "%"  .   $_GET['name'] . "%",
            ':Email'   => "%"  .   $_GET['email'] . "%",
            ':Password'   => "%"  .   $_GET['password'] . "%",
            ':Created_at'   => "%"  .   $_GET['created_at'] . "%"
        );

        $query = "select * from users where id like :Id or name like :Name or email like :Email
                  or password like :Password or created_at like Created_at";

        $statement = $connect->prepare($query);
        $statement = execute($data);
        $result  = $statement->fetchAll();
        foreach($result as $row){
            $output[] = array(
                'Id'        => $row['id'],
                'Name'      => $row['name'],
                'Email'     => $row['email'],
                'Password'  => $row['password'],
                'Created at'=> $row['created_at']
            );
        }
        echo json_encode($output);
    }
}   
1

There are 1 best solutions below

1
On

Check you MySQL query:

"select * from users where id like :Id or name like :Name or email like :Email
                  or password like :Password or created_at like Created_at"

You should add '' in search parameters:

'...where id like '".$data[':Id']."'...'