我有以下型号
类别:
<?php
class Category extends Eloquent {
protected $table = "category";
protected $fillable = array('title','parent','metatit','metadsc','metake','metaurl','image');
public function categoryitems(){
return $this->hasMany('CategoryItem','catid');
}
public function parent(){
return $this->hasMany('category','parent');
}
public function child(){
return $this->belongsTo('Category','parent');
}
}
需要在类别表中设置一对多关系
Ex 类别“城市”是类别“国家”的子类别
当我尝试使用以下代码时发生错误
<?php
$parent = Category::where('id','=',$cat->id)->parent;
echo $parent->title;
?>
错误 :
错误异常(E_UNKNOWN)
未定义的属性: Illuminate\Database\Eloquent\Builder::$parent (查看:/var/www/phpWithAngulerJS/app/views/admin/category-edit.blade.php)
首先,修复关系如下:
public function children() {
return $this->hasMany('Category','parent');
}
public function parent() {
return $this->belongsTo('Category','parent');
}
并且您的查询需要首先执行:
$parent = Category::where('id','=',$cat->id)->first()->parent;
// btw since you have $cat, you probably can do simply:
$cat->parent;
echo $parent->title;
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)