মডেল ট্রি
SuperAdmin\Admin\Tree ক্লাসটি ড্র্যাগ-অ্যান্ড-ড্রপ রিঅর্ডারিংসহ হায়ারার্কিক্যাল ডেটা (প্যারেন্ট-চাইল্ড রিলেশনশিপ) প্রদর্শন করে।
সেটআপ
1. মাইগ্রেশন
আপনার টেবিলে parent_id এবং order কলাম থাকা প্রয়োজন:
Schema::create('categories', function (Blueprint $table) {
$table->id();
$table->integer('parent_id')->default(0);
$table->integer('order')->default(0);
$table->string('title');
$table->timestamps();
});
2. মডেল
ModelTree ট্রেইট ব্যবহার করুন:
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
use SuperAdmin\Admin\Traits\ModelTree;
class Category extends Model
{
use ModelTree;
protected $table = 'categories';
}
3. কন্ট্রোলার
use App\Models\Category;
use SuperAdmin\Admin\Layout\Content;
use SuperAdmin\Admin\Tree;
class CategoryController extends AdminController
{
public function index(Content $content)
{
$tree = new Tree(new Category());
$tree->branch(function ($branch) {
return "<strong>{$branch['title']}</strong>";
});
return $content
->title('Categories')
->body($tree);
}
}
কলামের নাম কাস্টমাইজ করা
যদি আপনার কলামগুলো ডিফল্ট থেকে ভিন্ন হয়:
class Category extends Model
{
use ModelTree;
// Default: 'parent_id'
protected $parentColumn = 'pid';
// Default: 'order'
protected $orderColumn = 'sort';
// Default: 'title'
protected $titleColumn = 'name';
}
ট্রি ব্রাঞ্চ ডিসপ্লে
$tree->branch(function ($branch) {
$icon = $branch['icon'] ? "<i class='fa {$branch['icon']}'></i>" : '';
return "{$icon} {$branch['title']}";
});
কোয়েরি কলব্যাক
$tree->query(function ($model) {
return $model->where('type', 'category');
});
সিলেক্ট অপশন
ট্রি ডেটা থেকে একটি সিলেক্ট ড্রপডাউন তৈরি করুন (ফর্মে ব্যবহারের জন্য উপযোগী):
$options = Category::selectOptions();
$form->select('parent_id', 'Parent')->options($options);
কাস্টম রুট লেবেলসহ:
$options = Category::selectOptions(null, 'Root Category');