Home > Software engineering >  How to get Unique value in blade file laravel
How to get Unique value in blade file laravel

Time:10-13

I want to fetch unique value for filters of all products.

My db structure as follows

id   Product category_id format   attribute
1     demo1      5         HD      test1
2     demmo2     4         SD      test3
3     demmo3     4         HD      test2
4     demmo4     3         HD      test3

I want add filters of format and attribute in product page. But in that HD comming 3 times. I want to display that one only.

I am not getting how to display only single time.

Below is my controller code:

$item = Item::where('active_status', 1)->where('status', "1");
$data['item_count'] = $item;
$data['item'] = $item->paginate(20);
return view('frontend.pages.explore', compact('data'));

Below is blade file

<div class="filter-btn">
  @foreach($data['item'] as $resolution)
      <a class="btn btn-white-outline display-4" href="">{{array_unique($resolution->format)}}</a>
  @endforeach
</div>

I am not getting how to display unique value only. Anyone have idea then let me know

CodePudding user response:

If I am correct about your query, then you need to use groupby to list the items in your controller.

$items = Item::groupBy('format')->get();

CodePudding user response:

since you are paginating your data, your "first page" might not have all the formats, so you have to do another query to your database:

$formats = DB::table('items')->select('format')->get()
...
view(..., compact('data', 'formats'))

in the blade table:

@foreach($formats as $resolution)
  <a class="btn btn-white-outline display-4" href="">{{array_unique($resolution)}}</a>
@endforeach
  • Related