首页 > 解决方案 > laravel 将 URL 添加到内爆数据

问题描述

我想知道如何为内爆数据链接结果?

代码

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

$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]

我想要的只是在这些类别名称中添加 slug 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

public function categories()
  {
    return $this->hasMany(Category::class);
  }

  public function childs() {
    return $this->hasMany(Category::class,'category_id','id') ;
  }

  public function parent()
  {
      return $this->belongsTo(Category::class,'category_id');
  }

  public function isParent()
  {
      return !$this->category_id ? true : false; // if category_id is null => is a Parent Category
  }
public function products(){
     return $this->hasMany(Product::class);
  }


//just added
public function getBreadCrumb(){
        $crumbs = array();

        // handle parent
        if(!empty($this->parent)){
            $crumbs[] = $this->parent->getBreadCrumb();  
        }

        $crumbs[] = '<a href="' . $this->slug . '">' . $this->title . '</a>';
        return implode(">", $crumbs);
    }

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

标签: phplaravelimplode

解决方案


一种方法可以是......

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(),它就会为您提供所有线索以及类别


推荐阅读