yajra / laravel-datatables

jQuery DataTables API for Laravel
https://yajrabox.com/docs/laravel-datatables
MIT License
4.76k stars 859 forks source link

Service Implementations Relations #1856

Closed GerarPacheco closed 1 year ago

GerarPacheco commented 6 years ago

Hello I have a pretty strange problem ... I do not know what may be happening and I hope you can help me solve my mistake I'm using the datatable service implementation Laravel 5.6

public function query(Medico $model){
   return $model->with(['especialidad','tipo','usuario'])->newQuery();
}

When I call the columns in this way, the only problem I get is that they do not execute the relationship with the users table

protected function getColumns()
    {
        return [
            ['data' => 'nombre', 'title' => 'Nombre'],
            ['data' => 'apellidos', 'title' => 'Apellidos'],
            ['data' => 'tipo.nombre', 'title' => 'Tipo de Médico'],
            ['data' => 'tipo_area_medica', 'title' => 'Tipo de Área Médica'],
            ['data' => 'area_medica', 'title' => 'Área Médica'],
            ['data' => 'especialidad.nombre', 'title' => 'Especialidad'],
        ];
    }

The answer via xhr is correct

{
    "data": [{
        "id": "1",
        "user_id": "15",
        "codigo": "1013-2016-1665051",
        "codigo_msp": null,
        "codigo_senescyt": null,
        "especialidad_id": "86",
        "tipo_medico_id": "1",
        "consultorio_id": "5",
        "firma": "",
        "firma_tipo": null,
        "valor_consulta": "10.00",
        "duracion_consulta": "30",
        "observacion": null,
        "created_at": "2018-09-25 14:21:35",
        "updated_at": "2018-09-25 14:21:35",
        "deleted_at": null,
        "especialidad": {
            "id": "86",
            "area_medica_id": "1",
            "codigo": "0000000522",
            "nombre": "PSICOLOGIA (CE)",
            "created_at": "2018-09-25 14:21:33",
            "updated_at": "2018-09-25 14:21:33",
            "deleted_at": null
        },
        "tipo": {
            "id": "1",
            "nombre": "NOMBRAMIENTO PROVISIONAL",
            "observaciones": null,
            "created_at": null,
            "updated_at": null,
            "deleted_at": null
        },
        "usuario": {
            "id": "15",
            "dni": "1203991888",
            "tipo_documento": "C\u00c9DULA",
            "nombre": "MILTON LEONARDO",
            "apellidos": "MORA AYALA",
            "email": "251978@HOTMAIL.ES",
            "password": "$2y$10$7p44IZwZ6jyay4ksaZ4Nj.BYgiULuZxiH3WP90EJX4dEI7TKUvWiW",
            "telefono": "",
            "celular": "968416370",
            "direccion": "COCANIGUAS Y RUILOVA",
            "provincia_id": "1",
            "ciudad_id": "1",
            "parroquia_id": "1",
            "fecha_nacimiento": "05-02-1978",
            "genero": "MASCULINO",
            "foto": "default.jpg",
            "estado_civil": "SOLTERO(A)",
            "ultimo_acceso": "26-09-2018 07:36",
            "ultima_salida": "26-09-2018 07:36",
            "created_at": "2018-09-25 14:21:35",
            "updated_at": "2018-09-25 14:21:35",
            "deleted_at": null
        },
        "area_medica": "CONSULTA EXTERNA",
        "tipo_area_medica": "MEDICINA CONVENCIONAL",
        "nombre": "MILTON LEONARDO",
        "apellidos": "MORA AYALA",
    }]
}

But when I refer to the user relationship in the columns, the data is overwritten.

protected function getColumns()
    {
        return [
            ['data' => 'nombre', 'title' => 'Nombre','name'=>'usuario.nombre'],

Staying in this way

{
        "id": "15",
        "user_id": "15",
        "codigo": "1013-2016-1665051",
        "codigo_msp": null,
        "codigo_senescyt": null,
        "especialidad_id": "86",
        "tipo_medico_id": "1",
        "consultorio_id": "5",
        "firma": "",
        "firma_tipo": null,
        "valor_consulta": "10.00",
        "duracion_consulta": "30",
        "observacion": null,
        "created_at": "2018-09-25 14:21:35",
        "updated_at": "2018-09-25 14:21:35",
        "deleted_at": null,
        "dni": "1203991888",
        "tipo_documento": "C\u00c9DULA",
        "nombre": "MILTON LEONARDO",
        "apellidos": "MORA AYALA",
        "email": "251978@HOTMAIL.ES",
        "password": "$2y$10$7p44IZwZ6jyay4ksaZ4Nj.BYgiULuZxiH3WP90EJX4dEI7TKUvWiW",
        "telefono": "",
        "celular": "968416370",
        "direccion": "COCANIGUAS Y RUILOVA",
        "provincia_id": "1",
        "ciudad_id": "1",
        "parroquia_id": "1",
        "fecha_nacimiento": "1978-02-05",
        "genero": "MASCULINO",
        "foto": "default.jpg",
        "estado_civil": "SOLTERO(A)",
        "ultimo_acceso": null,
        "ultima_salida": null,
        "remember_token": null,
        "especialidad": {
            "id": "86",
            "area_medica_id": "1",
            "codigo": "0000000522",
            "nombre": "PSICOLOGIA (CE)",
            "created_at": "2018-09-25 14:21:33",
            "updated_at": "2018-09-25 14:21:33",
            "deleted_at": null,
            "area_medica": {
                "id": "1",
                "codigo": "CE001",
                "nombre": "CONSULTA EXTERNA",
                "tipo_area_medica_id": "1",
                "created_at": "2018-09-25 14:21:33",
                "updated_at": "2018-09-25 14:21:33",
                "deleted_at": null,
                "tipo": {
                    "id": "1",
                    "codigo": "CONV001",
                    "nombre": "MEDICINA CONVENCIONAL",
                    "created_at": "2018-09-25 14:21:33",
                    "updated_at": "2018-09-25 14:21:33",
                    "deleted_at": null
                }
            }
        },
        "tipo": {
            "id": "1",
            "nombre": "NOMBRAMIENTO PROVISIONAL",
            "observaciones": null,
            "created_at": null,
            "updated_at": null,
            "deleted_at": null
        },
        "usuario": {
            "id": "15",
            "dni": "1203991888",
            "tipo_documento": "C\u00c9DULA",
            "nombre": "MILTON LEONARDO",
            "apellidos": "MORA AYALA",
            "email": "251978@HOTMAIL.ES",
            "password": "$2y$10$7p44IZwZ6jyay4ksaZ4Nj.BYgiULuZxiH3WP90EJX4dEI7TKUvWiW",
            "telefono": "",
            "celular": "968416370",
            "direccion": "COCANIGUAS Y RUILOVA",
            "provincia_id": "1",
            "ciudad_id": "1",
            "parroquia_id": "1",
            "fecha_nacimiento": "05-02-1978",
            "genero": "MASCULINO",
            "foto": "default.jpg",
            "estado_civil": "SOLTERO(A)",
            "ultimo_acceso": "26-09-2018 07:39",
            "ultima_salida": "26-09-2018 07:39",
            "created_at": "2018-09-25 14:21:35",
            "updated_at": "2018-09-25 14:21:35",
            "deleted_at": null
        },
        "area_medica": "CONSULTA EXTERNA",
        "tipo_area_medica": "MEDICINA CONVENCIONAL",
}

Thanks for your time Bye

RomkaLTU commented 6 years ago

I have done this with joins.

$q = Registries::query()->where('metas->name', $this->registry_name)
            ->join('clients','registries.metas->client_id','=','clients.id');

So no "with()" as I couldn't reach it in column definition.

github-actions[bot] commented 1 year ago

This issue is stale because it has been open for 30 days with no activity. Remove stale label or comment or this will be closed in 7 days.

github-actions[bot] commented 1 year ago

This issue was closed because it has been inactive for 7 days since being marked as stale.