Skip to content
Advertisement

Want to compare two columns of different table in laravel

I want to compare two columns from two different tables, I have column called “Features” in Model Attrition_correlators and another “Topcorrelators” in Membersdetail. Since I am new to laravel not sure how I can get the value and compare both values. Here is my code:

Controller.php

public function show(Memberdetails $Memberdetail, Attrition_correlators $Attrition_correlation_list)
    {
        
        return view('admin.members.Detailspage',compact('Memberdetail','Attrition_correlation_list'));
    }

 <div class="card-body p-0">
    <table class="table table-striped">
      @foreach(explode(',', $Memberdetail->Topcorrelators) as $row)
      <tr>
        <td>
          {{ $row }}
        </td>
         @if($Attrition_correlation_list->Features == $Memberdetail->Topcorrelators)
        <td>
          1
        </td>
        @else
        <td>
          0
        </td>
        @endif
   </tr>
   @endforeach

I want to compare the data which I am getting $row with the values in “features” if they match Want to get the value of correlation which is in Model “Attrition_correlators” under . Can anyone help me out in this! Thanks in Advance Below is the error I am getting enter image description here

Advertisement

Answer

It’s because you get a collection of Attrition_correlation_list. In this case you need to iterate over it to get properties:

@foreach(explode(',', $Memberdetail->Topcorrelators) as $row)
    <tr>
        <td>
            {{ $row }}
        </td>
        @foreach($Attrition_correlation_list as $item)
            @if($item->Features == $row)
            ...
            @else
            ...
            @endif
        @endforeach
   </tr>
   @endforeach

You also need to edit your controller like below. Because you passed null value in $Attrition_correlation_list

public function show(Memberdetails $Memberdetail)
    {
        $Attrition_correlation_list = Attrition_correlators::all();
        return view('admin.members.Detailspage',compact('Memberdetail','Attrition_correlation_list'));
    }
Advertisement