搜索

首页  >  问答  >  正文

一个 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粉025632437255 天前430

全部回复(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
  • 取消回复