Information from multiple tables in a foreach loop. laravelle 8
P粉025632437
P粉025632437 2024-03-19 17:02:42
0
1
407

I think try creating a table that is passed through a foreach loop that has to pick related data from two separate tables.

Actual situation, making a loan management web application, I have to show the lender the bid data he made to the borrower, but in order to make the display table complete, it needs information from two different tables in the database, what should I do willing

My person in charge Loan Controller

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]);


}

Use foreach loop to view the page 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>

Responsible table Loan Request

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');
        });

as well as user

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();
        });

So what I really want in the loop is to be able to call the beggar's actual username into the table.

P粉025632437
P粉025632437

reply all(1)
P粉604507867

The problem is that you are using the same collection as the variable

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

In this code, see your @foreach($userbids as $userbids) using the same name. Just change the code

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

laravel get() function returns a collection instead of an array in case you want to change it to an array

$userbids = Loan_requests::where('id',$ubids)->get()->toArray();
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!