【发布时间】:2018-01-05 10:23:57
【问题描述】:
我想通过选择表单组合框(例如 PHP 帖子)返回用户请求的帖子列表。
所以我想返回post_title,post_body,我可以返回。
但我也想返回users 表中的user_name(帖子所有者)。
我在Posts 和Users 之间创建了一对一的关系,假设单个用户只能发布一个帖子
我要退货了
return \App\Post::where('post_topic', 'PHP')->get();
而我的 jquery ajax 是
$( document ).ready(function() {
$('#post_form').on('submit', function (e) {
e.preventDefault();
var post_topic = $('#post_select').val();
$.ajax({
type: "GET",
url: '/test',
data:{class: post_topic},
dataType : 'json',
success: function( data ) {
console.log(data);
var htmlText = '';
for ( var key in data ) {
htmlText += '<tr>';
htmlText += '<td class="">' + data[key].post_title + '</td>';
htmlText += '<td class="">' + data[key].post_body + '</td>';
htmlText += '<td class="">' + data[key].post_owner + '</td>';
htmlText += '</tr>';
}
$('#post_list').html(htmlText);
}
});
});
});
但这会使 user_name 未定义。
【问题讨论】: