dook0034 2018-06-19 00: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-19 01: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();
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 神经网络怎么把隐含层变量融合到损失函数中?
  • ¥30 自适应 LMS 算法实现 FIR 最佳维纳滤波器matlab方案
  • ¥15 lingo18勾选global solver求解使用的算法
  • ¥15 全部备份安卓app数据包括密码,可以复制到另一手机上运行
  • ¥15 Python3.5 相关代码写作
  • ¥20 测距传感器数据手册i2c
  • ¥15 RPA正常跑,cmd输入cookies跑不出来
  • ¥15 求帮我调试一下freefem代码
  • ¥15 matlab代码解决,怎么运行
  • ¥15 R语言Rstudio突然无法启动