Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to solve the problem of laravel sorting failure

2025-04-03 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly introduces "how to solve the problem of laravel sorting failure". In the daily operation, I believe many people have doubts about how to solve the problem of laravel sorting failure. The editor consulted all kinds of data and sorted out simple and easy-to-use operation methods. I hope it will be helpful for you to answer the doubts of "how to solve the problem of laravel sorting failure". Next, please follow the editor to study!

The solution to the failure of laravel sorting: 1. Through "$query- > whereIn (...)" Query data; 2, filter data through filter; 3, set sorted data to "$data = $scoutModelsLists;".

This article operating environment: Windows7 system, Laravel5.8 version, Dell G3 computer.

How to solve the problem of laravel sorting failure?

Laravel 5.8+scout7.0 uses orderBy sorting failure solution

Recently, when I was using elasticSearch7.2.4 to do a search, I found that the sort field was invalid, so in this record

Take a look at the solution first, say nothing, and go straight to the code.

$list = Article::search ($words)-> orderBy ('created_at','desc')-> paginateRaw (10)-> toArray (); $results = $list [' data']; if ($results ['hits'] [' total'] = = 0) {return $this- > model- > newCollection ();} $builder = new Builder (new static (), $this- > model- > newModelQuery ()); $keys = collect ($results ['hits'] [' hits'])-> pluck ('_ id')-> values ()-> all (); $query = $this- > newQuery () If ($builder- > queryCallback) {call_user_func ($builder- > queryCallback, $query);} / / query data $scoutModelsLists = $query- > whereIn ($this- > model- > qualifyColumn ($this- > model- > getKeyName ()), $keys)-> orderBy ('created_at','desc')-> get (); / / filter data $scoutModelsLists- > filter (function () use ($keys) {return in_array ($this- > model- > getKey (), $keys);}); / / here is the final sorted data $data = $scoutModelsLists

Analysis of problems

The query statement originally used is

$list = Article::search ($words)-> orderBy ('created_at','desc')-> paginate (10)-> toArray ()

Although the above query statement sets the sort field, there is no sort in the final output. After analysis, it is indeed sorted in the ES search results, but in the final output, when the ES data structure is transformed into a collection, no sort field is added.

Code analysis

File 1: / vendor/laravel/scout/src/builder.php approximately 261 lines-305 lines

If you take a closer look at this file, you will see that there are two methods, paginate and paginateRaw. The former returns the laravel collection and the latter returns the native query structure of es.

The difference between the two codes is that this piece

$results = $this- > model- > newCollection ($engine- > map ($this, $rawResults = $engine- > paginate ($this, $perPage, $page), $this- > model)-> all ())

File 2:vendor/tamayo/laravel-scout-elastic/src/ElasticsearchEngine.php 211line, map method, because here we are using the ES engine, if you use something else, it may be different, the code:

Public function map (Builder $builder, $results, $model) {/ / No data returns empty collection if ($results ['hits'] [' total'] = 0) {return $model- > newCollection ();} / / get all ES data with key _ id / / $keys = collect ($results ['hits'] [' hits'])-> pluck ('_ id')-> values ()-> all () / / convert ES data and filter return $model- > getScoutModelsByIds ($builder, $keys)-> filter (function ($model) use ($keys) {return in_array ($model- > getScoutKey (), $keys);});}

From the point of view of the code, if es searches for data, it will convert and filter the returned sets that meet the criteria. It is not satisfied to return empty data directly.

File 3: / vendor/laravel/scout/src/Searchable.php about 171lines of getScoutModelsByIds method, code

Public function getScoutModelsByIds (Builder $builder, array $ids) {/ / add soft deletion $query = static::usesSoftDelete ()? $this- > withTrashed (): $this- > newQuery (); if ($builder- > queryCallback) {call_user_func ($builder- > queryCallback, $query);} / / key here, change the code / / return $query- > whereIn (/ / $this- > getScoutKeyName (), $ids//)-> orderBy ('orderBy','desc')-> get () / / official code return $query- > whereIn ($this- > getScoutKeyName (), $ids)-> get ();}

This file is the key point, mainly because the orderBy sorting field is not added when it is returned here, so although the es is sorted and reset here in the final output, in order to prevent other places from being unable to update after the reorganization, sorting processing is added when the data is finally returned. Refer to the beginning of the article for the solution.

At this point, the study on "how to solve the problem of laravel sorting failure" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report