0

I have some Categories. When I will click on a category, then all of it's sub categories will be shown in a Bootstrap Modal.

<p data-toggle="modal" data-id="{{$category['id']}}" data-target="#subcategory_Modal">{{$category['name']}}</p>

I am passing the category id with data-id. Main Category has parent id 0. And sub category has a value of parent_id.

Now, How can I compare my passing ID: data-id with the parent_id which I am getting from database with Laravel/PHP.

@foreach($categories as $category)
    @if(!empty($category['sub_categories']))
      @foreach($category['sub_categories'] as $sc)
          @php($parent_id_of_sc = $sc['parent_id'])
              @if($parent_id_of_sc==id) //like this
                 <p><button>{{$sc['name']}}</button></p>
              @endif
      @endforeach
    @endif
@endforeach

Please check my code. I am getting all parent ids from this loop.I have to compare with the Modal Data ID which I have passed with Modal.

Tithira
  • 634
  • 9
  • 22
kazi Raushan
  • 49
  • 1
  • 7

1 Answers1

0

I am not a Laravel person, but comparing a string extracted from the DOM with the value of a PHP variable means sending the string to the server first.

You can do this by grabbing the string value from data-id using ELement.getAttribute("data-id")

https://developer.mozilla.org/en-US/docs/Web/API/Element/getAttribute

let category = document.getELementById("yourElementId").getAttribute("data-id")

or

var category = document.getELementById("yourElementId").getAttribute("data-id")

Then, find a way to send it to your PHP applicaton, usually by AJAX (with the XmlHttpRequest object) or some other means.

Anthony Rutledge
  • 6,980
  • 2
  • 39
  • 44