【问题标题】:Laravel / PHP - Push multiple values to query string (same parameter)Laravel / PHP - 推送多个值以查询字符串(相同的参数)
【发布时间】:2020-10-07 11:01:24
【问题描述】:

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

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

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

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

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

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

例如

我可以建造

website.com?category=plants&tags=hardy 

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

我无法建造


.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
}


在刀片注释中,它们会这样称呼

//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>

这让我发疯了,大多数网站、电子商务、旅游网站都允许你更新查询字符串,我觉得这种方法最容易从网站的任何地方调用,并且不会被绑定到特定页面, 功能,它还使构建器可扩展和可重用于更多标签。

但我就是想不通。

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

干杯

【问题讨论】:

  • 在 PHP 中查询字符串数组遵循语法 tags[]=tag1&amp;tags[]=tag2 并且 laravel 确实可以使用它,或者您可以将它们作为逗号分隔传递,例如tags=tag1,tag2 并将它们拆分为代码。仅此而已,可能还有其他 hacky 方法可以按照您尝试的方式进行操作,但只需使用 PHP 和 Laravel 提供的工具
  • 你为什么不这样添加标签:&tags=one,two,three并用逗号展开?

标签: php html laravel post query-string


【解决方案1】:

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

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

添加标签功能:

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 
} 

【讨论】:

  • 谢谢。这正是我正在寻找的。我只是在努力弄清楚如何构造这样的查询字符串。因此,我正在尝试弄清楚如何更新此功能。 function add_query_params(array $params = []) { $query = array_merge( request()-&gt;query(), $params ); return url()-&gt;current() . '?' . http_build_query($query); // rebuild the URL with the new parameters array } 和这个动作add_query_params(['tags' =&gt; $tag-&gt;id])
  • 如果你有一个变量被传递给视图,你可以试试$variable-&gt;appends(request()-&gt;all()),它应该附加你之前请求的参数
  • @AdamCatlow 这里是一个 sn-p collabedit.com/jsdhu
  • 这很棒。非常感谢,它真的帮助我指明了正确的方向! :D
【解决方案2】:

你不能那样做。这就像你有一个变量重新分配了一个新值。旧的将丢失。

你可以做的是传递一个逗号分隔的字符串,例如:

tags=test1,test2,test3

作为查询参数中的值。然后使用explode() 函数将其制成一个数组并使用这些值。

另一个想法是你的查询参数被创建为一个数组,所以你应该有这样的东西:

tags[]=test1&tags[]=test2&tags[]=test3

这样,当您请求参数 tags 时,您已经有一个数组可供使用,并且无法使用您的第一种方法。

所以你实际上在数组tags中插入了新值

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-10-11
    • 1970-01-01
    • 1970-01-01
    • 2017-12-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多