dook0034 2018-06-18 16:06
浏览 90
已采纳

Laravel Spatie查询生成器

In the index of my user controller I have the following:

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

I was hoping to pass an include param like this:

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

To append the withTrashed() global scope to the query.

Is this possible? I am most likely missing something obvious, I have tried a few variations in my testing usually ending up with an error like:

"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,

For reference: https://github.com/spatie/laravel-query-builderenter link description here

  • 写回答

1条回答 默认 最新

  • douhezhang8932 2018-06-18 17:14
    关注

    After checking out that library, here's what I've got.

    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();
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
编辑
预览

报告相同问题?

手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部