2016-11-04 56 views
0

联接的列我有以下代码:如何返回列,按照一定的顺序(雄辩)

Incident::where('id','<','35')->with('priority')->paginate(15); 

眼下这个返回如下: ID,标题,描述,priority_id和优先对象。

我想只检索ID,标题和优先级对象,没有描述,也没有priority_id但我希望它们按照特定的顺序,如下所示: ID,优先级和标题。

但是,当我做到以下几点:

Incident::where('id','<','5')->with('priority')->paginate(15, array('id', 'priority', 'title')); 

我得到一个错误说没有发现列incidents.priority。

有没有什么办法只选择我想要的列和我想要的顺序,当他们中的一个通过FK被引用?

谢谢!

+0

这可能是帮助you.check此链接https://packagist.org/packages/witty/laravel-table-view – Shuvo

+0

选中此:http://stackoverflow.com/questions/23612221/how-to-exclude -sertains-columns-while-using-eloquent –

+0

我无法想象一个场景,依赖于数组的顺序 – tam5

回答

0

你并不需要在列表中包括priorityIncident::where('id','<','5')->with('priority')->paginate(15, array('id', 'title'));

+0

但是,我不会在我想要的地方获得优先权:'( –

+0

@SofiaGilardino,你能告诉你在回收的收藏中做什么? –

0

如果你传递一个回调到with方法,你可以指定一个像这样的顺序:

Incident::where('id','<','35') 
    ->with(['priority' => function ($query) { 
     $query->select('id', 'priority', 'title'); 
    }]) 
    ->paginate(15); 
0

您的查询不加入。 with('priority')实际上是第二个单独的查询,它在Incident查询后执行,然后附加到Incident模型。如果你想引用列,并使用一个连接你会做这样的:

Incident::select('id', 'priorities.*', 'incidents.title') 
    ->leftJoin('priorities', 'priorities.id', '=', 'incidents.priority_id') 
    ->where('incidents.id', '>', '5') 
    ->paginate(15); 

如果你不想使用上述然后@tam答案将是最好的,但要确保他的解决方案,包括id在子查询回调中,因为这是关系如何在查询运行后将关系附加到父模型。