Laravel Scope Query抛出Grammar.php异常


Laravel Scope Query throwing Grammar.php exception

我在模型中设置了以下范围:

public function scopeInbox($query, $order = 'desc'){
    $userId = Auth::user()->id;
    return $query
        ->where('current_inbox','=',$userId)
        ->join('users', 'messages.created_by', '=', 'users.id')
        ->join('users as forwarder', 'messages.forwarded_by', '=', 'forwarder.id')
        ->select('messages.*', 'users.name', 'forwarder.name as forwarder')
        ->orderBy('updated_at', $order);
}

以及它的相关控制器中的以下内容:

public function inbox()
{
    $messages = Message::inbox();
    return response($messages->get());
}

Laravel不断抛出以下异常:

Argument 1 passed to Illuminate'Database'Grammar::columnize() must be of the type array, string given

有什么想法吗,S/O?

您有两个名为"转发器"的别名,其中一个是用户加入,另一个是转发器名称。

在某种逻辑循环中,当您声明"forwarder.name AS forwarder"时,您正试图使用与该表相同的别名对别名表的属性进行别名。

这可能不是导致你错误的原因,但这在语法上令人困惑,尽管在技术上是有效的。这是我的猜测,但我还没有证实

博格丹的评论是正确的。需要更多信息。发生错误的文件和行号以及可能的完整堆栈跟踪可能会有所帮助。

博格丹的回答确实是正确的。这个问题不是来自我对范围查询的实现,而是与我的路由设置方式有关。我设置了以下资源路线:
Route::resource('messages', 'MessagesController',
    ['only' => [
        'index',
        'store',
        'show',
        'destroy'
        ]
    ]
);

这使我的收件箱路由无法使用:

Route::get('messages/inbox', 'MessagesController@inbox');

这里的解决方案是将资源路线分解为单独的获取/发布路线。

Route::get('messages/index', 'MessagesController@index');
Route::post('messages/store', 'MessagesController@store');
Route::get('messages/show', 'MessagesController@show');
Route::get('messages/destroy', 'MessagesController@destroy');

我不确定是否有办法将自定义控制器方法定义为可访问资源,但我在Laravel文档中找不到任何东西。所以,这个解决方案是有效的,但我肯定会接受另一个答案,它提供了一个更合适的解决方案。

无论如何,谢谢你的洞察力,S/O!