Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Undefined property in Laravel using Eloquent

I have a problem when use Eloquent.

This is the error message.

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

This is My model. class Beneficiary extends 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');
  }
}

And this is my Model.

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

In this line occurs the error $refunds = $beneficiary->refunds;

Any idea?

like image 703
Josué H. Avatar asked Aug 14 '14 23:08

Josué H.


1 Answers

When you do

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

Contains a Collection of models, not a Model, so you have to:

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

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

Or

$beneficiary = Beneficiary::where('rut',Input::get('rut'))->first();
like image 121
Antonio Carlos Ribeiro Avatar answered Nov 14 '22 23:11

Antonio Carlos Ribeiro