简体   繁体   English

如何在 Laravel eloquent 中优先处理结果

[英]How to prioritize result in laravel eloquent

I have a table with 4 columns.我有一个有 4 列的表。 I want to display the row based on input and it should be able to display with priority according to the priority fields.我想根据输入显示行,它应该能够根据优先级字段优先显示。

example: My table looks like this:示例:我的表如下所示:

ID  title          description    tags
1   web developer  designer       front-end
2   designer       front-end      web developer
3   front-end      web developer  designer

I have my test cases below how it should work:我在下面有我的测试用例它应该如何工作:

$input = 'web developer' // the sequence should be IDs : 1, 3, 2
$input = 'front-end'     // output sequence result is : 3, 2, 1
$input = 'designer'      // result sequence : 2, 1, 3

Now, I want to prioritize the result from title down to tags based in the input.现在,我想根据输入对从标题到标签的结果进行优先级排序。

$blog = DB::('blogs')
        ->where('title', 'LIKE', '%$title%')
        ->where('description', 'LIKE', '%$title%')
        ->where('tags', 'LIKE', '%$title%');

But the above codes, seems that it didn't..但是上面的代码,似乎没有..

I think the logic you want to order your result should be appearing in an ORDER BY clause.我认为您想要对结果进行排序的逻辑应该出现在ORDER BY子句中。 Consider the following raw query:考虑以下原始查询:

SELECT *
FROM yourTable
ORDER BY
    CASE WHEN title = 'web developer'       THEN 0 ELSE 1 END,
    CASE WHEN description = 'web developer' THEN 0 ELSE 1 END,
    CASE WHEN tags = 'web developer'        THEN 0 ELSE 1 END;

Output:输出:

显示排序结果的图像

Here is my attempt at what the Laravel code would look like using orderByRaw :这是我尝试使用orderByRaw来查看 Laravel 代码的样子:

$orderByClause  = "CASE WHEN title = '".$input."' THEN 0 ELSE 1 END,";
$orderByClause .= "CASE WHEN description = '".$input."' THEN 0 ELSE 1 END,";
$orderByClause .= "CASE WHEN tags = '".$input."' THEN 0 ELSE 1 END";

$blog = DB::('blogs')
        ->orderByRaw($orderByClause)
        ->get();

I couldn't find any (working) documentation on how to parameterize an orderByRaw , so I used string concatenation instead.我找不到任何关于如何参数化orderByRaw (工作)文档,所以我改用字符串连接。

Demo here:演示在这里:

Rextester雷克斯特

Not a good solution but this might help you,不是一个好的解决方案,但这可能会帮助你,

$input = 'web developer';

$byTitle = DB::('blogs')
           ->where('title', 'LIKE', '%$input%')
           ->select('id');

$byDescription = DB::('blogs')
                 ->where('description', 'LIKE', '%$input%')
                 ->select('id');

$byTag = DB::('blogs')
         ->where('tag', 'LIKE', '%$input%')
         ->select('id');

$ids = $byTitle->union($byDescription)->union($byTag)->get();

You can select id according to column and then union of them to get data.您可以根据列选择 id,然后将它们并集以获取数据。

Here is what a raw query would look like if you wanted to handle this via a UNION :如果您想通过UNION处理这个问题,原始查询会是什么样子:

SELECT ID, 1 AS priority
FROM blogs
WHERE title = 'web developer'
UNION
SELECT ID, 2
FROM blogs
WHERE description = 'web developer'
UNION
SELECT ID, 3
FROM blogs
WHERE tags = 'web developer'
ORDER BY
    priority,
    ID

One caveat here is that if a given record had the same value for two or more of the title, description, or tags columns, then you would end up with duplicate records appearing in the result set.这里的一个警告是,如果给定记录的两个或多个标题、描述或标签列具有相同的值,那么您最终会在结果集中出现重复的记录。 From your original data, this does not appear to be happening, though this is an edge case worth mentioning.从您的原始数据来看,这似乎并没有发生,尽管这是一个值得一提的边缘情况。

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

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