php - Object exist BUT trying to get property on a non object -
im working on project on laravel5 php 5.5.12 , mysql 5.6.17 yesterday working fine, when come code today, have error "trying property of non-object" on multiple models objects, case occurs when have relationships other models.
if write :
$test = $model->relation->property;
i error.
if write :
echo / var_dump($model->relation->property);
then printed no problem.
i don't know happen , appreciated.
ps: sorry if im wrong text wrote, first time ask question on stackoverflow :) oh , did read similar questions
this 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 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 controller :
public function print_partner_invoice($partner_id, $echeance = 0) $partner = partner::find($partner_id); // 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 $key => $resa) { foreach ($resa->stays $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(); }
first, if write
var_dump($doesnotexists1->doesnotexists2->doesnotexists3);
its null, there no error, because var_dump looks $doesnotexists1, , if null not looking further. no "trying property of non-object in " execption.
so, problem maybe $stay or $stay->payment_due_date at
$amount = $stay->payment_due_date->total_amount;
is null/empty.
i check
foreach ($partner_resa $key => $resa) { foreach ($resa->stays $key => $stay) { if(is_object(stay) && is_object($stay->payment_due_date)) { $amount = $stay->payment_due_date->total_amount; } } }
to sure object
Comments
Post a Comment