簡體   English   中英

使用Eloquent在Laravel中的未定義屬性

[英]Undefined property in Laravel using Eloquent

使用Eloquent時遇到問題。

這是錯誤消息。

Undefined property: Illuminate\Database\Eloquent\Collection::$refunds

這是我的模特。 受益人延伸Eloquent {

public function refunds(){
    return $this->hasMany('Refund');
    }
} 

class Refund extends Model {
  protected $guarded = array();

  public static $rules = array(
    'request_id' => 'required',
    'beneficiary_id' => 'required',
    'concept' => 'required',
    'date' => 'required | date_format:Y-m-d',
    'amount' => 'required | min:-1 | numeric',
    'deductible_amount' => 'required | numeric',
    'max_applied' => 'required | numeric',
    'yearly_balance' => 'required | numeric',
    'payment_amount' => 'required | min:-1 | numeric',
    'payment_date' => 'required | date_format:Y-m-d',
);

  public function beneficiary(){
    return $this->belongsTo('Beneficiary','beneficiary_id');
  }

 public function request(){
    return $this->belongsTo('Models\Request','request_id');
  }
}

這是我的模特。

class HomeController extends BaseController {
   public function getIndex(){

    $requests = Requisition::with(array(
        'refunds' => function($refundsQuery){
                $refundsQuery->with(array(
                    'beneficiary' => function($beneficiaryQuery){
                            $beneficiaryQuery->with(array('beneficiary', 'holder'));
                        }
                ));
            },
        'policy' => function($policyQuery){
                $words=explode(' ',trim(Input::get('policy_code')));

                $policyQuery->where('code','LIKE','%'.$words[0].'%');

                for($i=1;$i<count($words);$i++){
                    $policyQuery->orWhere('code','LIKE','%'.$words[$i].'%');
                }

            },
        'refunds' => function($refundsQuery){
            $refundsQuery->with(array(
                'beneficiary' => function($beneficiaryQuery){
                    $beneficiaryQuery->with(array('beneficiary','rut'));
                }
            ));
        }
    ));



    if(Input::has('request_number')){
        $words=explode(' ',trim(Input::get('request_number')));

        $requests->where('number','LIKE','%'.$words[0].'%');

        for($i=1;$i<count($words);$i++){
            $requests->orWhere('number','LIKE','%'.$words[$i].'%');
        }
    }

    if(Input::has('policy_code')){
        $requests->whereHas('policy', function($policyQuery){
            $words=explode(' ',trim(Input::get('policy_code')));

            $policyQuery->where('code','LIKE','%'.$words[0].'%');

            for($i=1;$i<count($words);$i++){
                $policyQuery->orWhere('code','LIKE','%'.$words[$i].'%');
            }
        });
    }

    if(Input::has('rut')){
            $person = Person::where('rut', Input::get('rut'))->get();
            $beneficiary = Beneficiary::where('rut',Input::get('rut'))->get();
            $refunds = $beneficiary->refunds; //Error
    }

    $requests = $requests->paginate(10);

    return View::make('home.index',array(
        'requests'=>$requests,
        'policy_code' => Input::get('policy_code'),
        'request_number' => Input::get('request_number'),
        'rut' => Input::get('rut')
    ));

在此行中出現錯誤$ refunds = $ beneficiary-> refunds;

任何想法?

當你這樣做

$beneficiary = Beneficiary::where('rut',Input::get('rut'))->get();

包含模型集合,而不是模型,因此您必須:

$beneficiaries = Beneficiary::where('rut',Input::get('rut'))->get();

foreach($beneficiaries as $beneficiary)
{
   echo $beneficiary->refunds;
} 

要么

$beneficiary = Beneficiary::where('rut',Input::get('rut'))->first();

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM