I am try to get record e.g category table parent category related to sub category list and sub category inside to other sub category list in sort number of levels in sub category
I am try to create relation ship in model class but not work i sort data are not get in relationship function. check this code and replay
i am try this relation ship in model class
public function sub_category()
{
return $this->belongsTo(self::class, 'parent_id', 'id');
}
public function parent_category()
{
return $this->hasMany(self::class, 'id', 'parent_id');
}
But not work as my expectation. not get any data.
call this type of query
$category_data = Category::where('parent_id',0) ->get();
$category_data->sub_category; but error is undefined function sub_category also try this way
$category_data->sub_category();
last i am create function
Let me try this way as custom create function to manage array
public function sub_cat($data)
{
# code...
foreach ($data as $key => $value) {
# code...
$sub_category = $this->where('parent_id',$value->id)->get();
$sub = $sub_category;
if(!empty($sub)){
$sub = $this->sub_cat(json_decode($sub));
$value->sub = $sub;
}
}
return $data;
}
this type function work.
but my expectation to get data only add relationship
I am expected only use relationship to create array as parent category, child category, sub child category, sub to sub child category.
via Chebli Mohamed
Aucun commentaire:
Enregistrer un commentaire