使用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-&gt;退款;
有什么想法吗?
答案 0 :(得分:10)
当你这样做时
$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();