【发布时间】:2020-02-25 13:53:21
【问题描述】:
我使用 Eloquent 方法来处理表格。我没有使用查询和连接。只是与 hasMany 和 belongsTo 建立关系。而且我不想使用连接和查询。 我应该根据某些字段对结果进行排序。对主表字段进行排序没有任何问题。但是当我想按相关的表字段排序时,我得到了错误。
请求网址: http://localhost:8000/admin/pagination/fetch_data?page=1&sortby=product.product_title&sorttype=asc
请求方法:GET 状态码:500 内部服务器错误
这是我的代码。
评论
class Comment extends Model
{
public function user()
{
return $this-> belongsTo('App\User', "comment_userid");
}
public function confirmerUser()
{
return $this-> belongsTo('App\User', "comment_confirmeruserid");
}
public function product()
{
return $this->belongsTo("App\Product", "comment_productid");
}
}
产品
class Product extends Model
{
public function comments()
{
return $this->hasMany('App\Comment', "comment_productid");
}
}
用户
class User extends Authenticatable
{
public function comments()
{
return $this->hasMany('App\Comment', "comment_userid");
}
public function comments_confirmer()
{
return $this->hasMany('App\Comment', "comment_confirmeruserid");
}
}
在控制器中
public function controller_fetch_data(Request $request)
{
if( $request->ajax() ) {
$sort_by = $request->get('sortby');
$sort_type = $request->get('sorttype');
$comments = Comment::with('user', 'confirmerUser', 'product')
->orderBy($sort_by, $sort_type)->paginate(5);
$p_pagenumber = $request['page'];
return view("adminPanel.comment_list")
->with('p_pagenumber', $p_pagenumber)
->with('comments', $comments)
->render();
}
}
考虑中
function fetch_data(page, sort_type, sort_by)
{
$.ajax({
url : "/admin/pagination/fetch_data?page=" + page + "&sortby=" + sort_by + "&sorttype=" + sort_type,
success : function(data) {
$("#table_data").html('');
$("#table_data").html(data);
}
});
}
【问题讨论】:
-
你能分享一下错误吗?
-
请求 URL:localhost:8000/admin/pagination/… 请求方法:GET 状态码:500 内部服务器错误
-
500 非常抽象。在
storage/logs中查看您的日志并提供详细信息 -
我不确定你是否可以避免
join。看起来与:stackoverflow.com/questions/40837690/… -
我不确定。但是我认为当我使用hasMeny和belongsTo时,就不需要返回join和查询了。 Eloquent 必须支持所有这些类型的请求。但是如果您认为 Eloquent 不支持订购,请说我。 @Oluwatobi Samuel Omisakin