Prithvi Tuladhar
Prithvi Tuladhar

Reputation: 73

passing value to blade without use of foreach

i am trying to fetch a single row value using its' key in blade file table. how do it pass value to it. i have fetched the whole table in another blade file using foreach

i have passed the data from my controller but it doesn't recognize them.

loaninterest.blade.php

<div class="col-md-12">   
<table class="table table-stripped">
<thead>
  <tr>   
  <th>Loan Id</th>
  <th>Member</th> 
  @for($i=0;$i<$loan->duration; $i++ )
  <th>installment</th>
  @endfor
</tr>
<tr>
  <td>{{++$key}}</td>
  <td>{{$loan->member->name}}</td>
 @for($i=0;$i<$loan->duration; $i++ )
 <td> {{$loan->interest_amount}}  </td>
 @endfor
</tr>
  </thead>
  </table>
 </form>
        </div>

LoanController.php

  public function loanInterest(){
    $loanData = Loan::all();
    //$findUser =Loan::findOrFail($criteria);
    return view($this->_pagePath.'loan.loaninterest',compact('loanData'));
} 

web.php

 Route::any('loaninterest/{criteria?}','LoanController@loanInterest')-> name('loaninterest');

i called loaninterest blade file as below

loan.blade.php

   <td>
    <a href="{{route('loaninterest').'/'.$loan->id }}">Interest detail</a> 
  </td>

how do i pass loanData in blade. i get this error

Undefined variable:loan   

Upvotes: 1

Views: 1511

Answers (2)

Maulik Shah
Maulik Shah

Reputation: 1050

Why you pass url when you used route

<a href="{{route('loaninterest', $loan->id) }}">Interest detail</a> 

public function loanInterest(){
        // $loanData = Loan::all();
        $loanData =Loan::findOrFail($criteria); // or your where condition
        return view($this->_pagePath.'loan.loaninterest',compact('loanData'));
} 

In controller you are querying all data from table but you want only one data so you have to do findOrFail with unique id or use firstOrFail.

Try with this.

Hope this helps :)

Upvotes: 2

Prafulla Kumar Sahu
Prafulla Kumar Sahu

Reputation: 9693

Route parameters are send in a separate array try below code Syntax is

  route('routename', [parameter1, paramater2])

check docs

you can try

<a href="{{route('loaninterest', ['load' => $loan->id]) }}">Interest detail</a> 

for displaying loan data in view

 public function loanInterest(){
    $loanData = Loan::all();
    return view($this->_pagePath.'loan.loaninterest', compact('loanData')); //verify if $this->pagePath etc is correct
} 

In view

@foreach ( $loanData as $loanDeta )
    {{ $lanData->interest_amount }}
@endforeach

Upvotes: 1

Related Questions