简体   繁体   English

想比较laravel中不同表的两列

[英]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.我想比较两个不同表中的两列,我在 Model Attrition_correlators 中有一个名为“Features”的列,在 Membersdetail 中有另一个“Topcorrelators”。 Since I am new to laravel not sure how I can get the value and compare both values.由于我是 laravel 的新手,因此不确定如何获取值并比较这两个值。 Here is my code:这是我的代码:

Controller.php 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.我想将我得到的数据与“特征”中的值进行比较,如果它们匹配想要获得 Model“Attrition_correlators”下的相关值。 Can anyone help me out in this!谁能帮我解决这个问题! Thanks in Advance Below is the error I am getting在此先感谢以下是我得到的错误在此处输入图像描述

It's because you get a collection of Attrition_correlation_list.这是因为您获得了 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.您还需要编辑您的 controller,如下所示。 Because you passed null value in $Attrition_correlation_list因为您在$Attrition_correlation_list中传递了 null 值

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM