搜尋

首頁  >  問答  >  主體

一個 foreach 迴圈中來自多個表格的資訊。拉拉維爾 8

我認為嘗試建立一個傳遞 foreach 迴圈的表,該迴圈必須從兩個單獨的表中選取相關資料。

實際情況,製作貸款管理網絡應用程序,我必須向貸方顯示他向借款人提出的出價數據,但為了使顯示表格完整,它需要來自數據庫中兩個不同表格的信息,該怎麼辦我願意

我的負責人 貸款控制器

function beggers()
{
    // obtaining loop from different sources
    $user = auth()->user();
    $user_id = ($user->id);/* this calls for data of logged in user */

    $ubids = Bid::select('loan_id')->where('user_id',$user_id);
    $userbids = Loan_requests::where('id',$ubids)->get();
    
    $beg = Loan_requests::all();
    return view('beggers',['beg'=>$beg,'userbids'=>$userbids]);


}

使用foreach循環查看頁面 beggers.blade.php

#<h2>Deals Interested in</h2>
<table border="1">
    <tr>
        <td>Loan Id</td>
        <td>Begger</td> //this is the users name
        <td>Loan Type</td>
        <td>Amount</td>
        <td>View More</td>
        <td>status</td>
    </tr>

    @foreach ($userbids as $userbids)
    <tr>
        <td>{{ $userbids['id'] }}</td>
        <td>..</td>
        <td>{{ $userbids['LoanType'] }}</td>
        <td>{{ $userbids['amount'] }}</td>
        <td>..</td>
        <td>..</td>
    </tr>
    @endforeach
</table>

負責的表 貸款請求

Schema::create('loan_request', function (Blueprint $table) {
            $table->id();
            $table->unsignedBigInteger('users_id');
            $table->integer('LoanType');
            $table->Biginteger('amount');
            $table->string('PayType');
            $table->integer('IntervalPay');
            $table->string('GracePeriod');
            $table->timestamps();
            $table->foreign('users_id')
            ->references('id')->on('users')->ondelete('cascade');
        });

以及 用戶

Schema::create('users', function (Blueprint $table) {
            $table->id();
            $table->string('name');
            $table->string('email')->unique();
            $table->boolean('role')->nullable( );
            $table->timestamp('email_verified_at')->nullable();
            $table->string('password');
            $table->rememberToken()->nullable();
            $table->timestamps();
        });

所以我在循環中真正想要的是能夠將乞討者的實際使用者名稱呼叫到表中。

P粉025632437P粉025632437239 天前418

全部回覆(1)我來回復

  • P粉604507867

    P粉6045078672024-03-20 14:03:52

    問題是您使用與變數相同的集合

    @foreach ($userbids as $userbids)
        
            {{ $userbids['id'] }}
            ..
            {{ $userbids['LoanType'] }}
            {{ $userbids['amount'] }}
            ..
            ..
        
        @endforeach

    在此程式碼中,請參閱您使用相同名稱的@foreach($userbids as $userbids)。只需更改程式碼即可

    @foreach ($userbids as $userbid)
            
                {{ $userbid->id }}
                ..
                {{ $userbid->LoanType }}
                {{ $userbid->amount }}
                ..
                ..
            
            @endforeach

    laravel get() 函數傳回一個集合而不是數組,以防您想將其變更為數組

    $userbids = Loan_requests::where('id',$ubids)->get()->toArray();

    回覆
    0
  • 取消回覆