不使用foreach将值传递给刀片

问题描述 投票:1回答:2

我正在尝试使用其“刀片文件表中的密钥”来获取单行值。它是如何为它传递价值的。我使用foreach在另一个刀片文件中获取了整个表

我已经从我的控制器传递了数据,但它无法识别它们。

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

我把loaninterest刀片文件调用如下

loan.blade.php

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

我如何在blade中传递loanData。我收到这个错误

Undefined variable:loan   
laravel eloquent laravel-blade
2个回答
2
投票

为什么在使用路线时传递网址

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

在控制器中,您正在查询表中的所有数据,但您只需要一个数据,因此您必须使用唯一ID进行findOrFail或使用firstOrFail

试试这个。

希望这可以帮助 :)


1
投票

路由参数在单独的数组中发送尝试下面的代码语法是

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

检查docs

你可以试试

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

用于在视图中显示贷款数据

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

在视野中

@foreach ( $loanData as $loanDeta )
    {{ $lanData->interest_amount }}
@endforeach
© www.soinside.com 2019 - 2024. All rights reserved.