laravel - 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
... ...,
],
解决方案
尝试这个:
// 控制器文件:
$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
... ...,
],
推荐阅读
- xcode - 使用沙盒帐户测试 Mac 应用订阅 - 日期错误问题
- mysql - AWS 是否提供对 RDS MySQL 的支持?
- python - 如何在 python 中加入一个可迭代对象(字符串连接)?
- node.js - 为表 Postgres 续集缺少的 FROM 子句条目
- html - 带有 CSS 编码的 HTML
- react-native - 回答另一个调查问题回来后,我需要显示已检查的项目
- python - How to select multiple rows that have certain values in numpy?
- javascript - 将初始化对象传递给 setState React
- java - Array.copyOfRange 方法无法按预期运行并返回一个奇怪的字符串
- python - 如何在 python 中将叶图 html 读回叶图对象?