简体   繁体   中英

Not unique table alias in datatables Laravel

Im using Eloquent eager load to show all the childrens of an animal in a Yajra datatable, but Im getting this error:

在此处输入图片说明

My function where I get all the childrens:

public function hijos()
{
    return $this->belongsToMany('App\Ganado', 'hijos_padres', 'id_padre', 'id_hijo');
}

My datatable code:

    if ($request->ajax()) {

        return datatables()
            ->eloquent(Ganado::where('id_ganado', $id)->with('hijos'))    

            ->toJson();
    }

and this:

{data: 'hijos.id_ganado', name: 'hijos.id_ganado'},
{data: 'hijos.codigo_ganado', name: 'hijos.codigo_ganado'},
{data: 'hijos.genero', name: 'hijos.genero'},

My "hijos" (childrens) function have an itself relationship so I dont know how I can give an alias to the table.

This is my Animal table:

在此处输入图片说明

What else I can do?

The Problem is that the model is self referencing.

Solution: You should alias the table in the join.

Something like this:

Ganado::select('ganado.*')->leftJoin('ganado AS myAlias', 'ganado.mycolumn', '=', 'myAlias.other_column');

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM