我想知道如何关联内爆数据的结果?
此代码返回嵌套的我的产品类别(我将其用作面包屑)
ICE[1].average -
上面的代码结果如下:
$category = $product->category;
if(!empty($category->category_id)){
if(!empty($category->parent->category_id)){
$category = implode(" > ", [$category->parent->parent->title,$category->parent->title,$category->title]);
}else{
$category = implode(" > ", [$category->parent->title,$category->title]);
}
}else{
$category = $product->category->title;
}
我只想在这些类别名称Notebook > HP > HP Pavilion > [I place product name here]
中添加子弹。
我该怎么做?
Notebook, Hp, Hp Pavilion
product model
public function category(){
return $this->belongsTo(Category::class);
}
//just added
public function getBreadCrumb() {
// assuming that each product must be in some category,if not, add an empty check before adding into this array
$crumbs = array(
$this->category->getBreadCrumb(), // function in your `Category` model
'<a href="' . $this->slug . '">' . $this->title . '</a>'
);
return implode(">", $crumbs);
}
category model
............................................... ................................................... ................................................... ................................................... ................................................... ................................................... ................................................... ................................................... .....................
答案 0 :(得分:0)
做到这一点的一种方法是...
Product
模型创建一个简单的函数
public function getBreadCrumb() {
// assuming that each product must be in some category,if not, add an empty check before adding into this array
$crumbs = array(
$this->category->getBreadCrumb(), // function in your `Category` model
'<a href="' . $this->product_url . '">' . $this->product_title . '</a>'
);
return implode(">", $crumbs);
}
Category
模型 public function getBreadCrumb(){
$crumbs = array();
// handle parent
if(!empty($this->parent)){
$crumbs[] = $this->parent->getBreadCrumb();
}
$crumbs[] = '<a href="' . $this->category_url . '">' . $this->category_title . '</a>'
return implode(">", $crumbs);
}
现在,只要您需要,只需致电$product->getBreadCrumb()
,它就会带给您所有线索以及类别