An object exists, but trying to get a property on a non-object

Im working on a project on Laravel5 with PHP 5.5.12 and MYSQL 5.6.17 everything worked fine yesterday, but when I get back to my code today, I have the error "Trying to get a non-object property" for objects with multiple models, a case often occurs when they are related to other models.

if I write this:

$test = $model->relation->property; 

I get an error message.

If I write this:

 echo / var_dump($model->relation->property); 

then it is printed without problems.

I really don't know what will happen, and any help would be a GREAT appreciation.

Ps: I'm sorry if I am mistaken about the text that I wrote, I asked him the question about stackoverflow for the first time :) Oh, and I already read all such questions.

This is the Stay.php model:

 namespace App\models; use Illuminate\Database\Eloquent\Model; class Stay extends CustomModel { protected $table = 'stay'; protected $fillable = array('reservation_terms_convention_id', 'reservation_id', 'period_id', 'total_price', 'price_ht', 'occupant_id', 'prestation_fixe_id', 'pax', 'checkin', 'checkout'); protected $guarded = array('id'); static public $mailTemplatingWhitelist = ['total_price']; public static $rules = [ 'reservation_terms_convention_id' => 'required|exists:reservation_terms_convention,id', 'period_id' => 'required|exists:period,id', 'payeur_id' => 'required' ]; public function payment_due_date() { return $this->belongsTo('App\models\PaymentDueDate','payment_due_date_id', 'id'); } } 

here is a related model:

 <?php namespace App\models; use Illuminate\Database\Eloquent\Model; class PaymentDueDate extends CustomModel { protected $table = 'payment_due_date'; protected $fillable = array('total_amount', 'acompte_amount', 'amount_ht', 'amount_already_payed', 'payment_total_date', 'payment_acompte_date', 'facturation_date', 'description_facture', 'type', 'cash_desk_id', 'individual_customer_id', 'partner_id', 'direction'); protected $guarded = array('id'); public function stay(){ return $this->hasOne('App\models\Stay'); } } 

And finally, the controller:

 public function print_partner_invoice($partner_id, $echeance = 0) $partner = Partner::find($partner_id); // this return Collection of model $partner_resa = Partner::reservations_echeance($partner->id, $echeance); if(!$partner || $partner_resa->count() < 1) abort(403); $residence_company = $partner_resa->first()->residence->residence_company; $total_ttc = 0; $already_paid = 0; $tva_amount = 0; foreach ($partner_resa as $key => $resa) { foreach ($resa->stays as $key => $stay) { $amount = $stay->payment_due_date->total_amount; } } $pdf = \PDF::loadView('reglement.partner_invoice', [ 'partner' => $partner, 'resas' => $partner_resa, 'residence_company' => $residence_company, 'total_ttc' => $total_ttc, 'already_paid' => $already_paid, 'tva_amount' => $tva_amount ]); return $pdf->stream(); } 
+4
source share
1 answer

First if you write

 var_dump($doesnotexists1->doesnotexists2->doesnotexists3); 

its value is null, there will be no error because var_dump is looking for $ isnotexists1, and if its null value does not look any further. That way you are not getting an "Attempt to get a non-object property in" Execption ".

So your problem is that $ stay or $ stay-> payment_due_date in

  $amount = $stay->payment_due_date->total_amount; 

has the value null / empty.

i would see it as

 foreach ($partner_resa as $key => $resa) { foreach ($resa->stays as $key => $stay) { if(is_object(stay) && is_object($stay->payment_due_date)) { $amount = $stay->payment_due_date->total_amount; } } } 

to make sure this is an object

0
source