簡體   English   中英

想比較laravel中不同表的兩列

[英]Want to compare two columns of different table in laravel

我想比較兩個不同表中的兩列,我在 Model Attrition_correlators 中有一個名為“Features”的列,在 Membersdetail 中有另一個“Topcorrelators”。 由於我是 laravel 的新手,因此不確定如何獲取值並比較這兩個值。 這是我的代碼:

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

我想將我得到的數據與“特征”中的值進行比較,如果它們匹配想要獲得 Model“Attrition_correlators”下的相關值。 誰能幫我解決這個問題! 在此先感謝以下是我得到的錯誤在此處輸入圖像描述

這是因為您獲得了 Attrition_correlation_list 的集合。 在這種情況下,您需要對其進行迭代以獲取屬性:

@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

您還需要編輯您的 controller,如下所示。 因為您在$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