Id empty in GridConnector

Hi,
I’m trying to use the gridConnector in Laravel 5.3 but when I use it in my controller, it returns the rows without id. The code of my controller is this:


public function gridTecnicosAll() {                                    
        $connector = new GridConnector(null, "PHPLaravel");     
        $connector->configure(new User(), "id" ,"niu, name, email");                                           
		$connector->render();                                   
    }    

and it returns this:


<rows>
<row id="">
<cell>2135469</cell>
<cell>John teller</cell>
<cell>jhon@uab.cat</cell>
</row>
<row id="">
<cell>2135765</cell>
<cell>Michael Marimon</cell>
<cell>Miquel@uab.cat</cell>
</row>
</rows>

As you can see, the rows id are empty. I’ve tried to change the id value for the field “niu” and it is returning the same, an empty value. Here is the code of my table in the html page:


<div id="grid_here" style="width: 100%; height: 400px;"></div>
        <script type="text/javascript" charset="utf-8">
            mygrid = new dhtmlXGridObject('grid_here');
            mygrid.setHeader("NIU, Nombre, Email");
            mygrid.init();
            mygrid.load("/administracion/tecnicos/grid_all");
</script>
    </div>

Can you see what I’m doing wrong?

thanks!

Hi,

Can you share sources of User model ? By any chance, are you using a custom field name for storing User ID ?

Connector just selects all Users, and process the retrieved data as an array of data objects.
If model returns “id” field, it will be included in the output.

<?php

namespace App;

use Illuminate\Notifications\Notifiable;
use Illuminate\Foundation\Auth\User as Authenticatable;

class User extends Authenticatable
{
    use Notifiable;

    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = [
        'name', 'email', 'password', 'niu',
    ];

    /////DHTMLX Grid
    protected $table = "users";
    public $primaryKey = "id";
    public $timestamps = false;

    /**
     * The attributes that should be hidden for arrays.
     *
     * @var array
     */
    protected $hidden = [
        'password', 'remember_token',
    ];

    public function tecnico(){
        return $this->hasOne('App\Tecnico');

    }
}

I use the dafault field for User ID. I think it can’t be the problem because if I configure the connector like this:


$connector->configure(new User(), "niu" ,"id, name, email"); 

the program shows the ID.

I’ve solved this by creating 2 methods: one for retreiving data with a ->toArray() , and one for updating:

public function data() {
        $connector = new GridConnector(null, "PHPLaravel");
        $connector->configure(Book::all()->toArray(), "id", "title, author, sales");
        $connector->render();
    }

    public function update() {
        $connector = new GridConnector(null, "PHPLaravel");
        $connector->configure(new Book(), "id", "title, author, sales");
        $connector->render();
    }

Now id’s are not empty anymore.