【问题标题】:Laravel: how to query a column in a table that contains json data and return only the object that the query matchedLaravel:如何查询包含json数据的表中的列并仅返回查询匹配的对象
【发布时间】:2019-03-28 08:04:20
【问题描述】:

我正在做一个保存 JSON 数据的项目,其中每个对象有 3 个组件,并且有 50 到 200 个组件。

我想在数据库中的这个 JSON 列中搜索与对象组件中的单词或短语匹配的关键字,并返回具有匹配 JSON 数据的行。

已经构建了一个查询输入并将其连接到控制器、视图、模型等,我正在处理视图中显示的结果,我想返回包含与查询匹配的单个对象。

示例 json 结果

["{'result': [{'start': 5.00,", "'end': 10.0,", "'text': 'I would probably have dropped out of'},","{'start': 3.9,", "'end': 3.12,", "'text': 'college without knowing about trio that'},", "{'start': 3.42,", "'end': 5.49,", "'text': \"would have been my scenario if I've\"}]}"]

控制器功能

`   public function search(Request $request)
{
    $query = $request->input('query');
    $result = JSON::where('description', 'like', "%$query%")->first();
    $process = Collection::make($result->JSON);
    return view('view_JSON', ['result' => $result, 'query' => $query, 'process' => $process]);
}`

这是视图,我循环遍历集合和整个JSON数据,我想在这里返回与sql查询结果匹配的索引。

查看循环

@for($i=0; $i< count($process); $i++)
    {{   $process[$i]}}
@endfor

所以如果我的查询匹配 JSON 列中的大学并返回该行,我该如何简单地显示:

"{'start': 3.9,", "'end': 3.12,", "'text': 'college without knowing about trio that'},"

我认为保存 JSON 是 SQL 的一项新功能,因此我还不熟悉如何使用它。

【问题讨论】:

    标签: sql json laravel


    【解决方案1】:

    在这种情况下,您可以使用 Laravel Collection 类。为此,您需要将 json 转换为数组。

    使用 Mutators 将 json 转换为数组。

    正如documentation 所示,在您的模型配置中,您的字段将被强制转换为数组:

    CoolModel.php

    <?php
    
    namespace App;
    
    use Illuminate\Database\Eloquent\Model;
    
    class CoolModel extends Model
    {
        /**
         * The attributes that should be cast to native types.
         *
         * @var array
         */
        protected $casts = [
            'json_field' => 'array',
        ];
    }
    

    有了这个,你现在可以通过$coolModel-&gt;json_field 得到一个常规数组。

    现在,我们甚至可以收集这个数组,以使用 Laravel Collection 类:

    <?php
    
    namespace App;
    
    use Illuminate\Database\Eloquent\Model;
    
    class CoolModel extends Model
    {
    
        // The rest of your code 
    
        /**
         * Collect the json field.
         *
         * @param  string  $value
         * @return string
         */
        public function getJsonField($value)
        {
            return collect($value);
        }
    }
    

    如果一切顺利,现在您可以使用集合的强大功能来搜索您的术语:

    public function search(Request $request)
    {
        $query = $request->input('query');
        $result = CoolModel::where('description', 'like', $query)->first();
        $exact_result = $result->json_field->where('text', $query); // review this.
        $key = $exact_result->keys();
    
        return view('view_JSON', ['result' => $exact_result, 'key' => $key]);
    }
    

    【讨论】:

    • 我将在今天晚些时候尝试这个,但它对我来说看起来不错,很确定这就是我正在寻找的只是阅读你的答案。谢谢!
    • 是的,由于时间不够,我没有对其进行测试,但这应该可以工作,或者至少可以为您提供指导。 Collection 类包含许多有用的方法,可以在多个用例中使用。 this awesome book (by adam Wathan) 可以帮助您充分利用收藏。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-12-05
    • 1970-01-01
    • 2021-03-19
    • 2021-05-23
    相关资源
    最近更新 更多