【发布时间】:2017-02-09 12:02:24
【问题描述】:
我在App\User.php 中有以下hasMany() 关系,
public function partner_preference_occupation()
{
return $this-hasMany('App\Models\User\PartnerPreferenceOccupation', 'user_id');
}
以下是我的 PartnerPreferenceOccupation 模型,
<?php
namespace App\Models\User;
use App\Models\BaseModel,
App\Models\ValidationTrait;
class PartnerPreferenceOccupation extends BaseModel {
use ValidationTrait;
public function __construct() {
parent::__construct();
$this->__validationConstruct();
}
/**
* The database table used by the model.
*
* @var string
*/
protected $table = 'partner_preferences_occupation';
protected $fillable = array('user_id', 'occupation_id');
protected $dates = array();
public $uploadPath = array();
protected function setRules() {
$this->val_rules = array();
}
protected function setAttributes() {
$this->val_attributes = array();
}
public function occupation_name() {
return $this->belongsTo('App\Models\Master\OccupationModel', 'occupation_id');
}
}
我想在我的视图中显示职业名称数组。我尝试了以下代码,但失败了。
{{$obj->partner_preference_occupation ? $obj-partner_preference_occupation->occupation_name->name : null}}
报错如下,
Undefined property:Illuminate\Database\Eloquent\Collection::$occupation_name
如何显示它们。在此先感谢。
【问题讨论】:
标签: arrays laravel-5.2 has-many