简体   繁体   English

Laravel Spatie 查询生成器

[英]Laravel Spatie Query Builder

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.withTrashed()全局范围附加到查询。

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-builder enter link description here供参考: https ://github.com/spatie/laravel-query-builder在此处输入链接描述

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();

A late note that this is now natively supported in the package.一个后期注意,这现在在包中原生支持。

https://spatie.be/docs/laravel-query-builder/v5/features/filtering https://spatie.be/docs/laravel-query-builder/v5/features/filtering

QueryBuilder::for(Booking::class)
    ->allowedFilters([
        AllowedFilter::trashed(),
    ]);

// GET /bookings?filter[trashed]=only will only return soft deleted models

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM