Hi Coder,
Thank You For This Cool Components.
I'm Trying To Accomplish Category And SubCactegory Tree Menu With Laravel.
I want Import It From Laravel Like This
{
"objectID": "123",
"name": "orange",
"categories": {
"lvl0": "fruits",
"lvl1": "fruits > citrus"
}
}
So That I Can Use it Like This Example
In My Project, I have Category Table And This Is The Structure Of The Table
Schema::create('categories', function (Blueprint $table) {
$table->increments('id');
$table->integer('category_id')->unsigned()->nullable(); // The Perent Id If It's Sub Category
$table->string('name', 60);
$table->string('description', 100);
$table->string('icon', 60)->nullable();
$table->string('image', 100)->nullable();
$table->boolean('status')->default(1);
$table->enum('type', ['group', 'store'])->default('store');
$table->foreign('category_id')->references('id')->on('categories');
$table->timestamps();
});
And On My Product Model
public function toSearchableArray()
{
/**
* Load the categories relation so that it's available
* in the laravel toArray method
*/
$array = $this->toArray();
if($this->category->whereNull('category_id'))
{
$array['category'] = [
'name' => $this->category->name,
];
}
if ($this->category->children()->exists()) {
$array['sub_categories'] = $this->category->children()->map(function ($data) {
return [
'name' => $data['name'],
];
});
}
return $array;
}
How I Can Achive That.
Thank You For Everything.
Hi @zymawy, can you paste here what the array look like in the end?
To explain what you need to do, I think you should get something close to this:
public function toSearchableArray()
{
$array = $this->toArray();
$mainCategoryName = $this->category->name;
$array['category'] = [
'lvl0' => $mainCategoryName,
];
if ($this->category->children()->exists()) {
$array['category']['lvl1'] = [];
foreach ($this->category->children() as $cat) {
$array['category']['lvl1'][] = "$mainCategoryName > ".$cat['name'];
}
}
return $array;
}
Note: the code is not tested, it's just an example to illustrate what you have to do here.
I'll close the issue for now as it's not a bug but please, let me know if that worked!
Hi @julienbourdeau, Thank You For Inspiring Me.
Your Example Is perfect It Just Doesn't Need To Make foreach.
Since The Method, toSearchableArray Is Iterating Over All The Records.
For Example, I Need Country And CityAssociate It With University
$results['name'] = $this->name;
$results['slug'] = $this->slug;
$results['image_covered'] = "/images/" . $this->image_covered;
$results['path'] = $this->path();
$results['city_name'] = $this->city->name;
$results['by_major'] = $this->majors()->pluck('name');
$results['by_country'] = $this->city->country->arabic_name;
$results['breif'] = strip_tags(str_limit($this->breif, 100, '...'));
$results['country'] = ["lvl0" => $results['by_country']];
$results['country']['lvl1'] = $results['country']['lvl0'] . " > " . $results['city_name'];
return $results;
Most helpful comment
Hi @zymawy, can you paste here what the array look like in the end?
To explain what you need to do, I think you should get something close to this:
Note: the code is not tested, it's just an example to illustrate what you have to do here.
I'll close the issue for now as it's not a bug but please, let me know if that worked!