php - 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);
}
..................................................... ..................................................... ..................................................... ..................................................... ..................................................... ..................................................... ..................................................... ..................................................... .....................
解决方案
一种方法可以是......
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()
,它就会为您提供所有线索以及类别
推荐阅读
- html - 如何使用 CSS 删除复选框周围的空白(填充:0px)?
- c++ - 保持应该代表相同功能的同步 glsl 代码和 c++ 代码
- reactjs - Redux 工具包 RTK 查询突变没有得到返回数据
- java - 从 ubidots 获取实时数据到 android 应用程序
- python - 为什么 datetime 对象没有毫秒单位?
- c - 输入字母值时循环导致无限循环(C编程)
- postgresql - archlinux 将 postgresql 从 11 升级到 13:没有 /opt/pgsql-11/bin/ 文件夹
- html - 如何在 HTML 中添加这样的线条边框?
- tpm - 对 ESYS_Initialize 的未定义引用
- python - Django - 从对象中获取对象