【问题标题】:Merge 2 Laravel query results in one object将 2 个 Laravel 查询结果合并为一个对象
【发布时间】:2018-06-14 02:13:20
【问题描述】:

我发现了很多类似的问题,但对我来说没有真正的解决方案。 我有一个关于 Laravel 5.4 的项目
现在,在我的控制器中,我正在为我的搜索准备过滤器。
所以,想象一下我想按我的Cv 模型的“位置”和“关于”字段过滤我的搜索。

所以,我发现每个这样做的人都有不同的价值

    $arrOfTags = explode(',', $request['position']);
 //separate basic search request(using some js modules that return a request with "," separator.
    //Basically, find a Cv where location of "positions" is distinct
    $filter0 = Cv::select('location')
                ->whereIn('position', $arrOfTags)
                ->groupBy('location')
                ->get();
    $filter1 = Cv::select('about')
                ->whereIn('position', $arrOfTags)
                ->groupBy('about')
                ->get();

现在,我想将这 2 个结果连接到名为 $filters 的对象中

然后,在我的 Blade 中使用它:

@foreach($filters as $filter)
    <li> <input type="checkbox" name="location" value="{{$filter->location}}"/> <span> {{$filter->location}} </span> </li> 
@endforeach

@foreach($filters as $filter)
    <li> <input type="checkbox" name="location" value="{{$filter->about}}"/> <span> {{$filter->about}} </span> </li> 
@endforeach

我尝试了对象的array_merge,创建了一个新的空对象并设置了这个对象和其他东西的参数,但没有成功。

【问题讨论】:

  • @VinayMulgund 它返回给我 htmlspecialchars() 期望参数 1 是字符串,给定对象(查看:/home/primo/jobiMarket/resources/views/filtersForSearch.blade.php)这意味着 resultObject 是空的。

标签: php laravel object


【解决方案1】:

在这种情况下,您可以使用unique() 收集方法。获取数据:

$filters = Cv::select('location', 'about')
    ->whereIn('position', $arrOfTags)
    ->get();

获取每列的唯一值并使用结果:

@foreach($filters->unique('location') as $filter)
    <li> <input type="checkbox" name="location" value="{{ $filter->location }}"/> <span> {{$filter->location}} </span> </li> 
@endforeach

@foreach($filters->unique('about') as $filter)
    <li> <input type="checkbox" name="about" value="{{ $filter->about }}"/> <span> {{ $filter->about }} </span> </li> 
@endforeach

【讨论】:

  • 请让我确定它按预期工作,那么您是否以正确的方式理解我?我已经有一个 $request['position'],接下来我需要获取属于 'positions' 的 'location'、'about' 列的所有唯一值。示例: $request['positions'] = "driver" ;所以,我需要从 Cv 模型中获取“关于”和“位置”的所有唯一值,其中“位置”=驱动程序
  • @priMo-ex3m 我想我理解正确。请测试此解决方案。
  • 所以,想象一下我在 DB 中有 3 条记录,表 CV:1)位置:'司机',关于:'abcd',位置:'阿尔巴尼亚'; 2)位置:'司机',约:'dred',位置:'Georgia'; 3)位置:'司机',约:'dred',位置:'阿尔巴尼亚';所以,我的过滤器必须是:国家:阿尔巴尼亚,格鲁吉亚关于:abcd,dred
  • 它有效,非常感谢您提供唯一正确的解决方案!
  • @priMo-ex3m 在这种情况下,您需要手动重新映射数组或集合。但无论如何,这样做是个坏主意,在大多数情况下您应该使用单个集合。
【解决方案2】:

你为什么要把它完成?

$locationFilters= Cv::select('location')
            ->whereIn('position', $arrOfTags)
            ->groupBy('location')
            ->get();

$aboutFilters= Cv::select('about')
            ->whereIn('position', $arrOfTags)
            ->groupBy('about')
            ->get();
return view('view name',compact('locationFilters','aboutFilters'));

那么在你看来:

@foreach($locationFilters as  $filter)
     <input value="{{$filter->location}}"/>
@endforeach

@foreach($aboutFilters as $filter)
     <input value="{{$filter->about}}"/>
@endforeach

【讨论】:

    【解决方案3】:

    在服务器端,你可以这样做

    $filters[] = $filter0->location;
    $filters[] = $filter1->about;
    

    在视图中,只打印数组的值而不是访问对象。

    【讨论】:

    • 我想使用 OOP,这就是原因。
    【解决方案4】:

    您可以简单地使用merge 方法来合并two 集合。 merge 方法将给定的数组或集合与原始集合合并。试试这个-

        $filters =  $filter0->merge($filter1);
    

    更多详情可以查看laravel官方documentation

    【讨论】:

    • 它不起作用。当我在视图中制作 {{$filters}} 时,它返回给我 htmlspecialchars() 期望参数 1 是字符串,给定对象(视图:/home/primo/jobiMarket/resources/views/filtersForSearch.blade.php)表示该对象为空。
    • 你试过 foreach($filters as $filter) 吗?或者你只是打印整个集合??
    猜你喜欢
    • 2013-07-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-12-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多