gpt4 book ai didi

php - 对象存在但试图获取非对象的属性

转载 作者:可可西里 更新时间:2023-10-31 23:46:58 26 4
gpt4 key购买 nike

我正在使用 PHP 5.5.12 和 MYSQL 5.6.17 在 Laravel5 上开发一个项目昨天一切正常,但是当我今天回到我的代码时,我在多个模型对象上遇到错误“尝试获取非对象的属性”,这种情况经常发生在它们与其他模型有关系时。

如果我这样写:

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

我得到了错误。

如果我这样写:

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

然后它的打印没有问题。

我真的不知道会发生什么,我们将不胜感激任何帮助。

Ps:抱歉,如果我写的文字有误,这是我第一次在 stackoverflow 上提问 :)哦,我确实已经阅读了所有类似的问题

这是 Stay.php 模型:

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

这是相关模型:

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

}

最后是 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();
}

最佳答案

首先,如果你写

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

它是空的,不会有错误,因为 var_dump 寻找 $doesnotexists1,如果它是空的,它就不会再找了。所以你不会得到“试图在”Execption 中获取非对象的属性。

所以,您的问题可能是 $stay 或 $stay->payment_due_date at

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

为 null/空。

我会检查一下

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;
}
}
}

确定它是一个对象

关于php - 对象存在但试图获取非对象的属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31719886/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com