Propriedade indefinida no Laravel usando Eloquent

Josué H.

Eu tenho um problema ao usar o Eloquent.

Esta é a mensagem de erro.

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

Este é o meu modelo. classe Beneficiary estende 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');
  }
}

E este é meu modelo.

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

Nesta linha ocorre o erro $ refunds = $ beneficiário-> reembolsos;

Qualquer ideia?

Antonio Carlos Ribeiro

Quando você faz

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

Contém uma coleção de modelos, não um modelo, então você deve:

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

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

Ou

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

Este artigo é coletado da Internet.

Se houver alguma infração, entre em [email protected] Delete.

editar em
0

deixe-me dizer algumas palavras

0comentários
loginDepois de participar da revisão

Artigos relacionados

Laravel Eloquent HasOne :: $ id Propriedade indefinida

Laravel: Propriedade indefinida: Illuminate \ Database \ Eloquent \ Relations \ HasMany :: $ game

Propriedade indefinida: Illuminate \ Database \ Eloquent \ Builder :: $ password no Laravel

Propriedade indefinida: Illuminate \ Database \ Eloquent \ Collection :: $ name Laravel 5.3

Erro de relacionamento do Laravel: Propriedade indefinida: Illuminate \ Database \ Eloquent \ Collection :: $ id na linha 1

Erro de relacionamento do Laravel: Propriedade indefinida: Illuminate \ Database \ Eloquent \ Collection :: $ id na linha 1

Laravel 5.8: Propriedade indefinida: Illuminate \ Database \ Eloquent \ Relations \ BelongsToMany :: $ usr_id

Propriedade indefinida do Laravel no trabalho

Transforme a matriz usando reduzir (propriedade indefinida)

Laravel - Propriedade indefinida: stdClass - consulta de pesquisa

Erro de `propriedade indefinida` em tarefas laravel

Relacionamento da categoria no laravel Propriedade indefinida: stdClass ::

Propriedade indefinida do Laravel: stdClass :: $ id error

Propriedade indefinida: App \ Ticket :: $ status laravel 7

Propriedade indefinida: App \ Services \ UserService :: $ app no Laravel

Erro de PHP: Propriedade indefinida: Illuminate \ Database \ Eloquent \ Collection :: $ retails

Propriedade indefinida: Illuminate \ Database \ Eloquent \ Relations \ BelongsTo :: $ center_name

Propriedade indefinida: Illuminate \ Database \ Eloquent \ Relations \ MorphMany :: $ title

Propriedade indefinida: Illuminate \ Database \ Eloquent \ Relations \ BelongsToMany :: $ balance

Laravel Eloquent - propriedade dinâmica

Laravel | Usando Eloquent hasManyThrough

Usando subconsultas no Eloquent / Laravel

Propriedade indefinida: App \ User :: $ login usando Jenssegers \ Mongodb

erro: propriedade indefinida ao buscar dados usando API

Propriedade indefinida: Illuminate \ Support \ Collection :: $ id no Laravel 5.2

laravel 5.4: Propriedade indefinida: Illuminate \ Validation \ Validator :: $ erros

laravel 5.4: Propriedade indefinida: Illuminate \ Validation \ Validator :: $ erros

Id da propriedade indefinida Vue ao usar o Laravel Echo

Laravel 6 | Propriedade indefinida: Illuminate \ Notifications \ ChannelManager :: $ container