gpt4 book ai didi

php - 使用 Eloquent Laravel 中的未定义属性

转载 作者:塔克拉玛干 更新时间:2023-11-03 06:02:42 25 4
gpt4 key购买 nike

我在使用 Eloquent 时遇到了问题。

这是错误信息。

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

这是我的模型。 类受益人扩展 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');
}
}

这是我的模型。

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

在这一行中出现错误 $refunds = $beneficiary->refunds;

有什么想法吗?

最佳答案

当你做的时候

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

包含模型集合,而不是模型,因此您必须:

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

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

或者

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

关于php - 使用 Eloquent Laravel 中的未定义属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25319114/

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