2

I found a lot of similar questions but without a real solution for me. I have a project on Laravel 5.4
Now, in my Controller I am preparing filters for my search.
So, Imagine that I want to filter my search by "location", and "about" fields of my Cv model.

So, I am finding distinct values of each one doing this

    $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();

Now, I want to concatinate this 2 results in object named $filters

And then, in my Blade to use it like :

@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

I tried array_merge of objects, creating a new empty object and set parameters of this objects and other stuff, but without success.

priMo-ex3m
  • 1,072
  • 2
  • 15
  • 29
  • https://stackoverflow.com/questions/455700/what-is-the-best-method-to-merge-two-php-objects This worked for me :) – Vinay Mulgund Jan 04 '18 at 11:05
  • @VinayMulgund it returns me htmlspecialchars() expects parameter 1 to be string, object given (View: /home/primo/jobiMarket/resources/views/filtersForSearch.blade.php) That means that resultObject is empty. – priMo-ex3m Jan 04 '18 at 11:09

4 Answers4

1

In this case you can use unique() collection method. Get the data:

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

Get unique values for each column and use results:

@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
Alexey Mezenin
  • 158,981
  • 26
  • 290
  • 279
  • Let me please get sure that it works at expected, so did u understand me in the right way? I already have an $request['position'], next I need to get all unique values of 'location', 'about' columns that belongs to 'positions'. Example : $request['positions'] = "driver" ; So, I need to get all unique values of 'about' and 'location' from Cv model, where 'position' = driver – priMo-ex3m Jan 04 '18 at 11:16
  • @priMo-ex3m I think I understood correctly. Please test this solution. – Alexey Mezenin Jan 04 '18 at 11:18
  • So, Imagine that I have 3 records in DB with table CV : 1) position : 'driver', about : 'abcd', location : 'Albania' ; 2) position : 'driver', about : 'dred' , location : 'Georgia' ; 3) position : 'driver', about : 'dred' , location: 'Albania'; So, my filters must be : Country : Albania, Georgia About : abcd, dred – priMo-ex3m Jan 04 '18 at 11:22
  • 1
    It works, thank you very much for the only correct solution! – priMo-ex3m Jan 04 '18 at 11:23
  • Just one more question. Is there a way of concatinating 2 collections (like in my question example) into one result object? – priMo-ex3m Jan 04 '18 at 11:28
  • @priMo-ex3m when you're using my code, you're executing only one query and get a single collection, so you don't need to concatenate it at all. – Alexey Mezenin Jan 04 '18 at 11:29
  • Yep, I perfectly understand that. I just want to know if it's possible and how in case I will need to use it (in another case,project). Thanks. – priMo-ex3m Jan 04 '18 at 11:42
  • 1
    @priMo-ex3m you'll need to manually remap arrays or collections in this case. But it's a bad idea to do this anyway, you should use a single collection in most cases. – Alexey Mezenin Jan 04 '18 at 11:50
0

You could simply use merge method to merge two collections. The merge method merges the given array or collection with the original collection. Try this-

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

For further details you can see the official laravel documentation

Sohel0415
  • 9,523
  • 21
  • 30
  • It doesn't work. When I'm making {{$filters}} on view, it returns me htmlspecialchars() expects parameter 1 to be string, object given (View: /home/primo/jobiMarket/resources/views/filtersForSearch.blade.php) That means that object is empty. – priMo-ex3m Jan 04 '18 at 11:13
  • did you try foreach($filters as $filter)?? or you just print the whole collection?? – Sohel0415 Jan 04 '18 at 11:26
0

Why are you just making it complected ?

$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'));

Then in your view:

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

@foreach($aboutFilters as $filter)
     <input value="{{$filter->about}}"/>
@endforeach
Mahdi Younesi
  • 6,889
  • 2
  • 20
  • 51
0

on the server side, you can do it as

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

on view, just print the value of the array instead of accessing the object.

Danyal Sandeelo
  • 12,196
  • 10
  • 47
  • 78