提问者:小点点

Laravel eloquent datatables SQLSTATE[42S22]:未找到列:1054未知列的扇区。地方位置\ where子句中的名称'


我正在做一个关于Laravel和Datatables的项目。我用的是雄辩。Datatables正确填充表,但在执行(Datatables)搜索时收到错误:

下一步\Database\QueryException:SQLSTATE[42S22]:找不到列:1054未知列“扇区”。地方“where子句”中的位置\u名称“。。。

你能推荐一个解决方案吗?

表结构:

--更改日志表

id                
username          
sector_id         
operation_id     
layer_category_id 
object_name

--扇区表

id         
sector_name 
location_id

--位置表

id
location_name

在数据表中,我使用Ajax

{data:'sector.location.location_name',name:'sector.location.location_name'},我收到错误“未找到列:1054未知列'sector.location.location_name',在'where子句'中”

如果我使用表的名称,而不是像这样使用对象的名称:

{data: 'sector.location.location_name', name: '**sectors.locations.location_name**'},

我仍然收到一个错误:

未找到列: 1054未知列'sectors.locations.location_name'在'其中的条款'

数据已正确加载到datatables表中,但搜索不起作用

//changelogs.blade.php

     var table = $('.data-table').DataTable({
            processing: true,
            serverSide: true,
            ajax: "{{ route('change_logs.index') }}",
            columns: [
                //{data: 'DT_RowIndex', name: 'DT_RowIndex'},
                {data: 'id', name: 'change_logs.id'},
                {data: 'user.name', name: 'user.name'},
                {data: 'user.username', name: 'user.username'},
                {data: 'sector.location.location_name', name: 'sector.location.location_name'},
                {data: 'sector.sector_name', name: 'sector.sector_name'},
                {data: 'layer_category.layer.layer_name', name: 'layer_category.layer.layer_name'},
                {data: 'layer_category.category_name', name: 'layer_category.category_name'},
                {data: 'object_name', name: 'object_name'},
                {data: 'operation.operation_name', name: 'operation.operation_name'},
                {data: 'action', name: 'action', orderable: false, searchable: false},
            ]
        });




//controller

    $data = ChangeLog::with('user','sector','operation','layer_category' )->select('change_logs.*');
    return Datatables::of($data)


//class ChangeLog
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class ChangeLog extends Model
{
      protected $fillable = [
        'username', 'sector_id','operation_id', 'layer_category_id', 'object_name'
    ];

    protected $hidden = [
    ];  

    public function location()
    {
        return $this->belongsTo('App\Location');
    }

     public function user()
    {
        //return $this->belongsTo('App\User');
        return $this->belongsTo('App\User', 'username', 'username');

    }

    public function sector()
    {
        return $this->belongsTo('App\Sector')->with('location');
    }

    public function operation()
    {
        return $this->belongsTo('App\Operation');
    }

    public function layer_category()
    {
        return $this->belongsTo('App\LayerCategory')->with('layer');
    }


}

//class Sector
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Sector extends Model
{
    protected $fillable = [
        'sector_name','location_id',
    ];

    protected $hidden = [

    ];
public function location()
    {
        return $this->belongsTo('App\Location');
    }
}

//class Location
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Location extends Model
{
    protected $fillable = [
        'location_name',
    ];

    protected $hidden = [
    ];

public function sectors()
    {
        return $this->hasMany('App\Sector');
    }
}

共1个答案

匿名用户

您是否定义了将位置映射到部门的关系?从表结构来看,一个位置可以有多个扇区,这就是为什么我们在扇区中有位置id。因此,希望您在各自的模型中定义这种关系。

应用\Sector.php

...

public function location() {
    return $this->belongsTo('App\Location');
}

应用程序\位置。php

...

public function location() {
    return $this->hasMany('App\Sector');
}