Laravel Spatie查询生成器

时间:2018-06-19 00:06:25

标签: php laravel laravel-5 spatie

在我的用户控制器的索引中,我有以下内容:

return QueryBuilder::for(User::class)
    ->with('phoneNumbers')
    ->allowedIncludes('trashed')
    ->get();

我希望通过这样的包含参数:

http://127.0.0.1:8000/v1/users?include=trashed

withTrashed()全局范围追加到查询中。

这可能吗?我很可能错过了一些明显的东西,我在测试中尝试了一些变化,通常最终会出现如下错误:

"message": "Call to a member function addEagerConstraints() on boolean",
    "exception": "Symfony\\Component\\Debug\\Exception\\FatalThrowableError",
    "file": "/Users/timwickstrom/Sites/Wavefire/api/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Builder.php",
    "line": 522,

供参考: https://github.com/spatie/laravel-query-builder enter link description here

1 个答案:

答案 0 :(得分:1)

在查看该图书馆之后,这就是我所拥有的。

return QueryBuilder::for(User::class)
    ->with('phoneNumbers') // <-- I think you can use `allowedIncludes` here.
    ->allowedFilters([ // <-- I believe that `withTrashed` is a scope query,
                       // so you can use this. You cannot use `allowedIncludes`
                       // because it works with relations.
        Filter::scope('withTrashed')
    ])
    ->get();