首页 > 解决方案 > Laravel 数据表关系错误打印

问题描述

问题是如何在视图中打印(显示)主题的名称。我试过这个主题->主题名称仍然有错误。

投票模式

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

主题模型

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

APIController@getColumnSearchData

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

看法

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

标签: laraveldatatableeloquent

解决方案


尝试这个:

// 控制器文件:

$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
  ... ...,
],

推荐阅读