【问题标题】:how can I implement infinite scroll with custom queries in Laravel?如何在 Laravel 中使用自定义查询实现无限滚动?
【发布时间】:2017-01-25 13:03:33
【问题描述】:

如何在 Laravel 中使用自定义查询实现无限滚动? 类似的东西:

$query = Select * from table join table2 on condition where condition order by c1 ;
$l = DB::select($query)->paginate(12);

我有 5 个字段 state、region、city、category 和 price_range。用户可以按任何字段 1 到 5 进行搜索。然后我有一些其他的 url 参数需要处理,比如排序。

因此,如果我使用自定义查询,那么我可以连接从 url 接收到的值,并一次运行查询。

这是我有的场景。最终查询看起来就像我在上面发布的那样。

TIA

【问题讨论】:

    标签: laravel pagination laravel-5.3


    【解决方案1】:

    好的,你可以像这样链接条件:

    $query = DB::table('table1')
    ->join('table2', 'table1.id', '=', 'table2.table_id');
    
    if ( $request->has('state') && $request->state != '' ) {
        //We have a state in the query and it's not empty
        $query->where('table1.state', '=', $request->state);
    }
    
    if ( $request->has('region') && $request->region != '' ) {
        //We have a region in the query and it's not empty
        $query->where('table1.region', '=', $request->region);
    }
    
    //We finished checking all our conditions
    //Finishing the query now...
    
    $l = $query->paginate(12);
    

    【讨论】:

    • 谢谢。查询生成器的问题是我正在运行一个搜索脚本,并且我需要根据条件在查询中进行某种连接。我猜每次根据条件编写查询并不是一个好主意。并且 DB::raw() 在该分页上返回一个数组不起作用。
    • 你可以试试第三个选项。它允许您运行原始查询;
    • 是的,但它不允许在其上使用 pagination()
    • 更新了答案。
    • 用数据库替换模型不起作用,Call to undefined method Illuminate\Database\MySqlConnection::selectRaw()
    猜你喜欢
    • 1970-01-01
    • 2019-07-23
    • 2023-04-10
    • 2015-05-18
    • 2017-08-16
    • 2023-02-21
    • 2019-09-23
    • 1970-01-01
    • 2022-06-16
    相关资源
    最近更新 更多