php - Laravel/PHP - 将多个值推送到查询字符串(相同参数)

标签 php html laravel post query-string

在构建查询字符串/参数时遇到障碍,我觉得它不应该这么复杂,但我想获得最佳实践建议。

我目前正在构建一个社区平台,允许/要求用户标记他们的内容。内容通过两个查询字符串参数进行标记。

请注意所有关系,包括附加/分离在内的帖子标记逻辑已完成,这纯粹是一个过滤系统问题。

类别 - 帖子必须有一个类别但只有一个类别,在搜索查询字符串时将按此附加。例如。 ?category=植物

标签 - 帖子可以有(可选)许多标签,这将附加到查询字符串中。例如。 ?tags=耐寒

到目前为止。我创建了两个函数(通过 helper.php 文件)“add_query_params”和“remove_query_params”。这些很有用,因为它允许我添加和删除 ?category 或 ?tag 而无需删除另一个。我的问题是,我似乎一辈子都想不出如何将多个相同的标签添加到查询字符串中,以允许我过滤多个参数选项!

例如

我可以 build

website.com?category=plants&tags=hardy 

// I can also remove either tag without affecting the other

我无法 build


.com?category=plants&tags=hardy&tags=perenial
// (I can't add two of the same tag to the query string to allow me to filter on these in the controller/request

允许我添加/删除标签的函数如下


/**
 * URL before:
 * https://example.com/orders/123?order=ABC009&status=shipped
 *
 * 1. remove_query_params(['status'])
 * 2. remove_query_params(['status', 'order'])
 *
 * URL after:
 * 1. https://example.com/orders/123?order=ABC009
 * 2. https://example.com/orders/123
 */
function remove_query_params(array $params = [])
{
    $url = url()->current(); // get the base URL - everything to the left of the "?"
    $query = request()->query(); // get the query parameters (what follows the "?")

    foreach($params as $param) {
        unset($query[$param]); // loop through the array of parameters we wish to remove and unset the parameter from the query array
    }

    return $query ? $url . '?' . http_build_query($query) : $url; // rebuild the URL with the remaining parameters, don't append the "?" if there aren't any query parameters left
}

/**
 * URL before:
 * https://example.com/orders/123?order=ABC009
 *
 * 1. add_query_params(['status' => 'shipped'])
 * 2. add_query_params(['status' => 'shipped', 'coupon' => 'CCC2019'])
 *
 * URL after:
 * 1. https://example.com/orders/123?order=ABC009&status=shipped
 * 2. https://example.com/orders/123?order=ABC009&status=shipped&coupon=CCC2019
 */
function add_query_params(array $params = [])
{

    $query = array_merge(
        request()->query(),
        $params
    ); // merge the existing query parameters with the ones we want to add


    return url()->current() . '?' . http_build_query($query); // rebuild the URL with the new parameters array
}


在 Blade 评论中,他们会这样称呼

//Add query param
<a href="{{add_query_params(['tags' => $tag->id]) }}"
                class="inline-block bg-gray-100  px-3 py-1 text-xs uppercase font-button">
                #{{$tag->name}}
</a>


//Remove query param
<a href="{{remove_query_params(['category']) }}"
                class="inline-block bg-gray-300  px-3 py-1 text-xs uppercase font-button">
                #{{$has_category->name}}
                            
 </a>

这让我抓狂,大多数网站、电子商务、旅游网站都允许您更新查询字符串,我认为这种方法最容易从网站的任何地方调用,并且与特定页面无关, 功能,它还使构建器可扩展并可重复用于更多标签。

但我就是无法解决。

有人对如何向查询字符串添加多个标签有任何提示或指导吗????

干杯

最佳答案

你可以试试这个:
.com?category=plants&tags=hardy,perenial.
通过
$tags = explode(',', request()->tags);

从查询字符串中获取 Controller 中的标签作为数组

添加标签的函数:

add_query_params(array $params = [])    {        
    // defining parameters being used in requests
    $tags = request()->tags;

    // param key (name) and its value being added
    $param = null
    $value = null;
    foreach($params as $key => $value){
        $param = $key;
        $value = $val;
    }

    // merging new ones
    switch ($key) {
        case 'tags':
            array_push($tags, $value)
            break;
        case 'some other param':
            // push value to array, like tags case
            break;
        default:
            break;
     }

     $query = array_merge(request()->query(),$tags);
     return http_build_query($query); 
     // rebuild the URL with the new parameters array 
} 

关于php - Laravel/PHP - 将多个值推送到查询字符串(相同参数),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64242682/

相关文章:

php - Laravel/PHP 显示变量的错误输出

php - 插入行,其单元格数量取决于前一行单元格数量

python - 如何扩展 django 用户默认模型数据库

javascript - Tabpanel 中每个选项卡上的 ExtJS 关闭按钮

html - 2 栏网站布局与完整的背景幻灯片或视频

php - 代客不使用 php.8 Nginx 返回 Bad Gateway

laravel - 在 Laravel 中连接两个表

php - 将字符串转换为 float 而不丢失精度

php - 如何递归迭代字母表?

php - 如何在数据库中查找与搜索字符串只有一个字符不同的记录?