laravel将URL添加到内爆数据

时间:2018-11-09 06:12:53

标签: php laravel implode

我想知道如何关联内爆数据的结果?

代码

此代码返回嵌套的我的产品类别(我将其用作面包屑)

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

............................................... ................................................... ................................................... ................................................... ................................................... ................................................... ................................................... ................................................... .....................

1 个答案:

答案 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(),它就会带给您所有线索以及类别