【问题标题】:Laravel ignoring multiple type of objects in queryLaravel在查询中忽略多种类型的对象
【发布时间】:2019-03-12 08:39:05
【问题描述】:

我试图忽略查询中特定类型的对象。有些类型的数据我禁止或删除了我不希望用户找到的数据。我的查询看起来像:

$posts = DB::table('posts')->where('content', 'like', '%' . $request['content'] . '%')
        ->where('isfutured', '!=', '0')
        ->orderByDesc('id')->get();

但现在我也不想用isfutured 的4 获取数据。我怎么做?还是我可以在 Post 模型中以某种方式阻止这些并执行我的常规 Eloquent 查询?

【问题讨论】:

    标签: php mysql laravel eloquent filtering


    【解决方案1】:

    使用查询生成器 用于多个条件 whereIn 或 whereNotIn

    $isfutured = [0,4]; // add in array which did not want to get 
    
    $posts = DB::table('posts')->where('content', 'like', '%' . $request['content'] . '%')
            ->whereNotIn('isfutured', $isfutured)
            ->orderByDesc('id')->get();
    

    使用 elequent

    $posts = Post::whereNotIn('isfutured', $isfutured)->where('content', 'like', '%' . $request['content'] . '%')->orderByDesc('id')->get();
    

    【讨论】:

    • 哦,这正是我一直在尝试使用 rawsql 的方法,而且要困难得多,我不知何故设法用 mysql 写了这个,但很快就被这个取代了。
    猜你喜欢
    • 2023-03-14
    • 1970-01-01
    • 1970-01-01
    • 2011-10-29
    • 1970-01-01
    • 1970-01-01
    • 2019-02-01
    • 2015-11-17
    • 1970-01-01
    相关资源
    最近更新 更多