如何在视图或刀片文件中访问模型功能

时间:2019-04-15 10:11:28

标签: laravel function eloquent model

我想要在表“贷款”中获得为金额和利率的倍数的利息金额。我想从表中调用该值并用于显示贷款信息。

我尝试使用增幅器,在这种情况下,会产生与下面提到的相同的错误

Loan.php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Loan extends Model
{
    protected $fillable = ['amount', 'interest', 'status', 'member_id', 'loan_type_id', 'interest_type_id', 'loan_payment_type_id'];

    public function getInterestAmountAttribute()
    {
        return $this->amount * $this->interest;
    }

    public function member()
    {
        return $this->belongsTo(Member::class, 'member_id', 'id');
    }
}

loan.blade.php

<table class="table table-stripped">
    <thead>
    <tr>
        <th>Loan Id</th>
        <th>Amount</th>
        <th>Interest</th>
        <th>Interest Amount</th>
        <th>Interest Type</th>
        <th>Loan Type</th>
        <th>Status</th>
        <th>Payment Type</th>
        <th>Member</th>
        <th>Action</th>
    </tr>

    @foreach($loanData as $key=>$loan)
        <tr>
            <td>{{++$key}}</td>
            <td>{{$loan->amount}}</td>
            <td>{{$loan->interest}}</td>
            <td>{{ $loan->interest_amount}}</td>
            <td>{{$loan->interesttype->interest_type}}</td>
            <td>{{$loan->loantype->loan_type}}</td>
            <td align="center">

                <span class="bg bg-primary" style="border-radius: 10px;padding:2px 5px">{{$loan->status}}</span>

                <form action="{{route('update-loan-status')}}" method="post">
                    {{csrf_field()}}
                    <input type="hidden" name="criteria" value="{{$loan->id}}"/>
                    @if($loan->status== 1)
                        <button class="btn btn-success btn-xs" name="paid"><i class="fa fa-check"></i></button>
                    @endif
                    @if($loan->status== 0)
                        <button class="btn btn-danger btn-xs" name="unpaid"><i class="fa fa-close"></i></button>
                    @endif
                </form>
            </td>
            <td>{{$loan->paymentmethod->method}}</td>
            <td>{{$loan->member->name}}</td>
            <td>
                <a href="">Delete</a>
                <a href="">Edit</a>
            </td>
        </tr>
    @endforeach
    </thead>
</table>
{{$loanData->links()}}

这会出现以下错误:

  

方法Illuminate \ Database \ Eloquent \ Collection :: links不存在。

当我除去括号

 <td>{{$loan->getInterest}}</td>

错误是

  

App \ Loan :: getInterest必须返回一个关系实例。

LoanController.php

<?php

namespace App\Http\Controllers\Backend;

use App\Http\Controllers\Controller;
use App\Loan;
use App\Interest;
use App\LoanPaymentMethod;
use App\Member;
use App\Loantype;
use DB;

class LoanController extends Controller
{
    protected $_backendPath = 'backend.';
    protected $_pagePath = 'backend.pages.';
    protected $_data = [];

    public function index()
    {
        $loanData = Loan::orderBy('id', 'desc')->get();
        $loanData = Loan::paginate(10);
        $loanData = Loan::all();
        $memberData = Member::all();
        $loantypeData = Loantype::all();
        $paymentmethodData = LoanPaymentMethod::all();
        $interestData = Interest::all();
        $this->_data['loanData'] = $loanData;

        //$results = Loan::with('member')->get();
        //$dat->loan = Loan::with('member')->get();
        //$loan = Member::find($memberData)->loan;

        return view($this->_pagePath . 'loan.loan', $this->_data);

        //$userData = DB::table('members')->get();
        //return view('home',compact('userData'));
    }
}

3 个答案:

答案 0 :(得分:1)

在Loan.php中定义访问器功能:

public function getGetInterestAttribute()
{
   return $this->amount * $this->interest;
}

现在您可以像这样访问它:

<td>{{ $loan->get_interest }}</td>

答案 1 :(得分:1)

调用访问器的正确方法,

public function getModifiedInterestAttribute()    // first get then ModifiedInterest then Attribute
{
  return $this->amount * $this->interest;
}

然后您可以按以下方式调用访问器,

<td>{{$loan->modified_interest }}</td>

您可以查看更多详细信息:https://laravel.com/docs/5.8/eloquent-mutators#defining-an-accessor

答案 2 :(得分:0)

您应该将该变量附加到模型数据中,例如

class Loan extends Model
{
     protected $appends = ['interest'];
}

您的访问者应该喜欢

public function getInterest()
{
  return $this->amount * $this->interest;
}

如上所用访问刀片文件

<td>{{$loan->interest}}</td>