Laravel数据表关系错误打印

时间:2018-08-16 08:26:57

标签: laravel datatable eloquent

  

问题是如何在View中打印(显示)主题名称。我尝试了这个topic-> topic_name仍然出错。

投票模式

 @PropertySource(value = "file:///${CONFIG_PATH}/folder/application-environment.properties")

主题模型

protected $fillable = [
    'topic_id' ,'question', 'answer', 'lastname', 'firstname', 'identity', 'user_id',
];
public function topic(){
    return $this->belongsTo('App\Topic');
}

APIController @ getColumnSearchData

protected $fillable = [
    'topic_name',
];
public function votes(){
    return $this->hasMany('App\Vote');
}

查看

$customers = Vote::select(['id', 'topic_id', 'question', 'answer','lastname','firstname','identity', 'user_id', 'created_at']);
return Datatables::of($customers)->make(true);

1 个答案:

答案 0 :(得分:1)

尝试一下:

//控制器文件:

$customers = Vote::with('topic')->get();
return Datatables::of($customers)->make(true);

//视图:

processing: true,
serverSide: true,
ajax: '{{ route('api.column_search') }}',
columns: [
  { data: 'id', name: 'id' },
  { data: 'topic.topic_name', name: 'topic.topic_name' },    there is error
  ... ...,
],