Home  >  Q&A  >  body text

Information from multiple tables in a foreach loop. laravelle 8

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粉025632437P粉025632437237 days ago414

reply all(1)I'll reply

  • P粉604507867

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

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

    reply
    0
  • Cancelreply