laravel adding URL to imploded data
up vote
0
down vote
favorite
I want to know how can I have linked result for imploded data?
Code
this code return my product category nested (I use this as breadcrumbs)
$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;
result of code above is like:
Notebook > HP > HP Pavilion > [I place product name here]
All I want is to add slugs in those category names Notebook, Hp, Hp Pavilion.
How can I do that?
Update
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);
..................................................................................................................................................................................................................................................................................................................................................................................................................................
php laravel implode
add a comment |
up vote
0
down vote
favorite
I want to know how can I have linked result for imploded data?
Code
this code return my product category nested (I use this as breadcrumbs)
$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;
result of code above is like:
Notebook > HP > HP Pavilion > [I place product name here]
All I want is to add slugs in those category names Notebook, Hp, Hp Pavilion.
How can I do that?
Update
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);
..................................................................................................................................................................................................................................................................................................................................................................................................................................
php laravel implode
1
Hi, your question is bit confusing in terms of what is actually required. Do you want to minimize this 4 line statement with a one line or do it in a more optimized way? Please explain.
– Farooq Khan
Nov 9 at 6:31
@FarooqKhan hi thanks for asking, my code return my categories titles I just want add slug<a></a>tag to those returned titles. so when I printNotebookit has link.
– mafortis
Nov 9 at 6:36
If I were to implement this I'd use it as a helper method in respective models asproduct->getBreadcrumb()and '$category->getBreadcrumb()'. Each of these is a function inModelwhere you can format it as per your needs. Let me know if you need further explaination about this.
– Farooq Khan
Nov 9 at 6:46
@FarooqKhan any way that can give me that result is fine by me, please share with me your solution if you may.
– mafortis
Nov 9 at 6:49
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
I want to know how can I have linked result for imploded data?
Code
this code return my product category nested (I use this as breadcrumbs)
$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;
result of code above is like:
Notebook > HP > HP Pavilion > [I place product name here]
All I want is to add slugs in those category names Notebook, Hp, Hp Pavilion.
How can I do that?
Update
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);
..................................................................................................................................................................................................................................................................................................................................................................................................................................
php laravel implode
I want to know how can I have linked result for imploded data?
Code
this code return my product category nested (I use this as breadcrumbs)
$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;
result of code above is like:
Notebook > HP > HP Pavilion > [I place product name here]
All I want is to add slugs in those category names Notebook, Hp, Hp Pavilion.
How can I do that?
Update
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);
..................................................................................................................................................................................................................................................................................................................................................................................................................................
php laravel implode
php laravel implode
edited Nov 9 at 7:11
asked Nov 9 at 6:12
mafortis
932634
932634
1
Hi, your question is bit confusing in terms of what is actually required. Do you want to minimize this 4 line statement with a one line or do it in a more optimized way? Please explain.
– Farooq Khan
Nov 9 at 6:31
@FarooqKhan hi thanks for asking, my code return my categories titles I just want add slug<a></a>tag to those returned titles. so when I printNotebookit has link.
– mafortis
Nov 9 at 6:36
If I were to implement this I'd use it as a helper method in respective models asproduct->getBreadcrumb()and '$category->getBreadcrumb()'. Each of these is a function inModelwhere you can format it as per your needs. Let me know if you need further explaination about this.
– Farooq Khan
Nov 9 at 6:46
@FarooqKhan any way that can give me that result is fine by me, please share with me your solution if you may.
– mafortis
Nov 9 at 6:49
add a comment |
1
Hi, your question is bit confusing in terms of what is actually required. Do you want to minimize this 4 line statement with a one line or do it in a more optimized way? Please explain.
– Farooq Khan
Nov 9 at 6:31
@FarooqKhan hi thanks for asking, my code return my categories titles I just want add slug<a></a>tag to those returned titles. so when I printNotebookit has link.
– mafortis
Nov 9 at 6:36
If I were to implement this I'd use it as a helper method in respective models asproduct->getBreadcrumb()and '$category->getBreadcrumb()'. Each of these is a function inModelwhere you can format it as per your needs. Let me know if you need further explaination about this.
– Farooq Khan
Nov 9 at 6:46
@FarooqKhan any way that can give me that result is fine by me, please share with me your solution if you may.
– mafortis
Nov 9 at 6:49
1
1
Hi, your question is bit confusing in terms of what is actually required. Do you want to minimize this 4 line statement with a one line or do it in a more optimized way? Please explain.
– Farooq Khan
Nov 9 at 6:31
Hi, your question is bit confusing in terms of what is actually required. Do you want to minimize this 4 line statement with a one line or do it in a more optimized way? Please explain.
– Farooq Khan
Nov 9 at 6:31
@FarooqKhan hi thanks for asking, my code return my categories titles I just want add slug
<a></a> tag to those returned titles. so when I print Notebook it has link.– mafortis
Nov 9 at 6:36
@FarooqKhan hi thanks for asking, my code return my categories titles I just want add slug
<a></a> tag to those returned titles. so when I print Notebook it has link.– mafortis
Nov 9 at 6:36
If I were to implement this I'd use it as a helper method in respective models as
product->getBreadcrumb() and '$category->getBreadcrumb()'. Each of these is a function in Model where you can format it as per your needs. Let me know if you need further explaination about this.– Farooq Khan
Nov 9 at 6:46
If I were to implement this I'd use it as a helper method in respective models as
product->getBreadcrumb() and '$category->getBreadcrumb()'. Each of these is a function in Model where you can format it as per your needs. Let me know if you need further explaination about this.– Farooq Khan
Nov 9 at 6:46
@FarooqKhan any way that can give me that result is fine by me, please share with me your solution if you may.
– mafortis
Nov 9 at 6:49
@FarooqKhan any way that can give me that result is fine by me, please share with me your solution if you may.
– mafortis
Nov 9 at 6:49
add a comment |
1 Answer
1
active
oldest
votes
up vote
0
down vote
One way to do this can be ...
Product Model
Create a simple function
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 Model
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);
And now, where ever you need it, just call $product->getBreadCrumb() and it will get you all the trail along with categories
it returnsCall to a member function getBreadCrumb() on stringon// handle parent if(!empty($this->parent)) $crumbs = $this->parent->getBreadCrumb();
– mafortis
Nov 9 at 7:06
I assumed that$category->parentwill be acategorytype object, but in your case it is a string. You must use parent_catgory object here instead of string
– Farooq Khan
Nov 9 at 7:08
I didn't understand what you said! please wait i'll share my models with you
– mafortis
Nov 9 at 7:09
updated........
– mafortis
Nov 9 at 7:12
in my blade i said$product->category->parentgot:"id":1,"title":"Notebook","description":null,"image":"category-1528762207.png","imageAlt":null,"image_header":"categoryheader-1528863834.jpg","image_header_alt":"header image","cheading":"Jual Notebook Murah dan bergaransi resmi","listed":"0","slug":"notebook","sort":"1","meta_description":null,"meta_tags":null,"status_id":1,"category_id":null,"created_at":"2018-06-12 14:10:07","updated_at":"2018-06-29 00:11:10"
– mafortis
Nov 9 at 7:17
|
show 7 more comments
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
0
down vote
One way to do this can be ...
Product Model
Create a simple function
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 Model
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);
And now, where ever you need it, just call $product->getBreadCrumb() and it will get you all the trail along with categories
it returnsCall to a member function getBreadCrumb() on stringon// handle parent if(!empty($this->parent)) $crumbs = $this->parent->getBreadCrumb();
– mafortis
Nov 9 at 7:06
I assumed that$category->parentwill be acategorytype object, but in your case it is a string. You must use parent_catgory object here instead of string
– Farooq Khan
Nov 9 at 7:08
I didn't understand what you said! please wait i'll share my models with you
– mafortis
Nov 9 at 7:09
updated........
– mafortis
Nov 9 at 7:12
in my blade i said$product->category->parentgot:"id":1,"title":"Notebook","description":null,"image":"category-1528762207.png","imageAlt":null,"image_header":"categoryheader-1528863834.jpg","image_header_alt":"header image","cheading":"Jual Notebook Murah dan bergaransi resmi","listed":"0","slug":"notebook","sort":"1","meta_description":null,"meta_tags":null,"status_id":1,"category_id":null,"created_at":"2018-06-12 14:10:07","updated_at":"2018-06-29 00:11:10"
– mafortis
Nov 9 at 7:17
|
show 7 more comments
up vote
0
down vote
One way to do this can be ...
Product Model
Create a simple function
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 Model
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);
And now, where ever you need it, just call $product->getBreadCrumb() and it will get you all the trail along with categories
it returnsCall to a member function getBreadCrumb() on stringon// handle parent if(!empty($this->parent)) $crumbs = $this->parent->getBreadCrumb();
– mafortis
Nov 9 at 7:06
I assumed that$category->parentwill be acategorytype object, but in your case it is a string. You must use parent_catgory object here instead of string
– Farooq Khan
Nov 9 at 7:08
I didn't understand what you said! please wait i'll share my models with you
– mafortis
Nov 9 at 7:09
updated........
– mafortis
Nov 9 at 7:12
in my blade i said$product->category->parentgot:"id":1,"title":"Notebook","description":null,"image":"category-1528762207.png","imageAlt":null,"image_header":"categoryheader-1528863834.jpg","image_header_alt":"header image","cheading":"Jual Notebook Murah dan bergaransi resmi","listed":"0","slug":"notebook","sort":"1","meta_description":null,"meta_tags":null,"status_id":1,"category_id":null,"created_at":"2018-06-12 14:10:07","updated_at":"2018-06-29 00:11:10"
– mafortis
Nov 9 at 7:17
|
show 7 more comments
up vote
0
down vote
up vote
0
down vote
One way to do this can be ...
Product Model
Create a simple function
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 Model
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);
And now, where ever you need it, just call $product->getBreadCrumb() and it will get you all the trail along with categories
One way to do this can be ...
Product Model
Create a simple function
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 Model
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);
And now, where ever you need it, just call $product->getBreadCrumb() and it will get you all the trail along with categories
edited Nov 9 at 7:26
answered Nov 9 at 7:03
Farooq Khan
1,23411229
1,23411229
it returnsCall to a member function getBreadCrumb() on stringon// handle parent if(!empty($this->parent)) $crumbs = $this->parent->getBreadCrumb();
– mafortis
Nov 9 at 7:06
I assumed that$category->parentwill be acategorytype object, but in your case it is a string. You must use parent_catgory object here instead of string
– Farooq Khan
Nov 9 at 7:08
I didn't understand what you said! please wait i'll share my models with you
– mafortis
Nov 9 at 7:09
updated........
– mafortis
Nov 9 at 7:12
in my blade i said$product->category->parentgot:"id":1,"title":"Notebook","description":null,"image":"category-1528762207.png","imageAlt":null,"image_header":"categoryheader-1528863834.jpg","image_header_alt":"header image","cheading":"Jual Notebook Murah dan bergaransi resmi","listed":"0","slug":"notebook","sort":"1","meta_description":null,"meta_tags":null,"status_id":1,"category_id":null,"created_at":"2018-06-12 14:10:07","updated_at":"2018-06-29 00:11:10"
– mafortis
Nov 9 at 7:17
|
show 7 more comments
it returnsCall to a member function getBreadCrumb() on stringon// handle parent if(!empty($this->parent)) $crumbs = $this->parent->getBreadCrumb();
– mafortis
Nov 9 at 7:06
I assumed that$category->parentwill be acategorytype object, but in your case it is a string. You must use parent_catgory object here instead of string
– Farooq Khan
Nov 9 at 7:08
I didn't understand what you said! please wait i'll share my models with you
– mafortis
Nov 9 at 7:09
updated........
– mafortis
Nov 9 at 7:12
in my blade i said$product->category->parentgot:"id":1,"title":"Notebook","description":null,"image":"category-1528762207.png","imageAlt":null,"image_header":"categoryheader-1528863834.jpg","image_header_alt":"header image","cheading":"Jual Notebook Murah dan bergaransi resmi","listed":"0","slug":"notebook","sort":"1","meta_description":null,"meta_tags":null,"status_id":1,"category_id":null,"created_at":"2018-06-12 14:10:07","updated_at":"2018-06-29 00:11:10"
– mafortis
Nov 9 at 7:17
it returns
Call to a member function getBreadCrumb() on string on // handle parent if(!empty($this->parent)) $crumbs = $this->parent->getBreadCrumb(); – mafortis
Nov 9 at 7:06
it returns
Call to a member function getBreadCrumb() on string on // handle parent if(!empty($this->parent)) $crumbs = $this->parent->getBreadCrumb(); – mafortis
Nov 9 at 7:06
I assumed that
$category->parent will be a category type object, but in your case it is a string. You must use parent_catgory object here instead of string– Farooq Khan
Nov 9 at 7:08
I assumed that
$category->parent will be a category type object, but in your case it is a string. You must use parent_catgory object here instead of string– Farooq Khan
Nov 9 at 7:08
I didn't understand what you said! please wait i'll share my models with you
– mafortis
Nov 9 at 7:09
I didn't understand what you said! please wait i'll share my models with you
– mafortis
Nov 9 at 7:09
updated........
– mafortis
Nov 9 at 7:12
updated........
– mafortis
Nov 9 at 7:12
in my blade i said
$product->category->parent got: "id":1,"title":"Notebook","description":null,"image":"category-1528762207.png","imageAlt":null,"image_header":"categoryheader-1528863834.jpg","image_header_alt":"header image","cheading":"Jual Notebook Murah dan bergaransi resmi","listed":"0","slug":"notebook","sort":"1","meta_description":null,"meta_tags":null,"status_id":1,"category_id":null,"created_at":"2018-06-12 14:10:07","updated_at":"2018-06-29 00:11:10"– mafortis
Nov 9 at 7:17
in my blade i said
$product->category->parent got: "id":1,"title":"Notebook","description":null,"image":"category-1528762207.png","imageAlt":null,"image_header":"categoryheader-1528863834.jpg","image_header_alt":"header image","cheading":"Jual Notebook Murah dan bergaransi resmi","listed":"0","slug":"notebook","sort":"1","meta_description":null,"meta_tags":null,"status_id":1,"category_id":null,"created_at":"2018-06-12 14:10:07","updated_at":"2018-06-29 00:11:10"– mafortis
Nov 9 at 7:17
|
show 7 more comments
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53220693%2flaravel-adding-url-to-imploded-data%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
1
Hi, your question is bit confusing in terms of what is actually required. Do you want to minimize this 4 line statement with a one line or do it in a more optimized way? Please explain.
– Farooq Khan
Nov 9 at 6:31
@FarooqKhan hi thanks for asking, my code return my categories titles I just want add slug
<a></a>tag to those returned titles. so when I printNotebookit has link.– mafortis
Nov 9 at 6:36
If I were to implement this I'd use it as a helper method in respective models as
product->getBreadcrumb()and '$category->getBreadcrumb()'. Each of these is a function inModelwhere you can format it as per your needs. Let me know if you need further explaination about this.– Farooq Khan
Nov 9 at 6:46
@FarooqKhan any way that can give me that result is fine by me, please share with me your solution if you may.
– mafortis
Nov 9 at 6:49