public function test_assigning_categories() { list($seller, $product) = $this->visitProductCategories(); // assign from scratch $categories = Category::allLeaves()->get()->random(2); foreach ($categories as $category) { $this->checkMulti($this->getCategoryInputId($category)); } $this->press('提交')->seeSuccessMessage(); foreach ($categories as $category) { $this->seeIsChecked($this->getCategoryInputId($category)); } // change categories $newCategories = Category::allLeaves()->whereNotIn('id', $categories->pluck('id')->toArray())->get()->random(2); foreach ($categories as $category) { $this->uncheckMulti($this->getCategoryInputId($category)); } foreach ($newCategories as $category) { $this->checkMulti($this->getCategoryInputId($category)); } $this->press('提交')->seeSuccessMessage(); foreach ($newCategories as $category) { $this->seeIsChecked($this->getCategoryInputId($category)); } // remove all categories foreach ($newCategories as $category) { $this->uncheckMulti($this->getCategoryInputId($category)); } $this->press('提交')->seeSuccessMessage(); foreach ($newCategories as $category) { $this->dontSeeIsChecked($this->getCategoryInputId($category)); } }
/** * Run the migrations. * * @return void */ public function up() { Schema::create('categories', function (Blueprint $table) { // These columns are needed for Baum's Nested Set implementation to work. // Column names may be changed, but they *must* all exist and be modified // in the model. // Take a look at the model scaffold comments for details. // We add indexes on parent_id, lft, rgt columns by default. $table->increments('id'); $table->integer('parent_id')->nullable()->index(); $table->integer('lft')->nullable()->index(); $table->integer('rgt')->nullable()->index(); $table->integer('depth')->nullable(); // Add needed columns here (f.ex: name, slug, path, etc.) // $table->string('name', 255); $table->string('name'); $table->string('slug'); $table->timestamps(); }); \App\Modules\CategoryModule\Entities\Category::buildTree($this->categories); }
/** * @param User|Shop $owner */ private function seedProducts($owner) { $products = $owner->products()->saveMany(factory(Product::class, 10)->make()); // assign to 1 or 2 categories $products->each(function (Product $product) { $categories = Category::leavesOnly()->take(mt_rand(1, 2))->get()->pluck('id'); $product->categories()->attach($categories->toArray()); }); // also create 2 inactive products $owner->products()->saveMany(factory(Product::class, 2)->make(['active' => false])); }
public function index(Product $product) { $categories = Category::all()->toHierarchy()->toArray(); return $this->view('category::{account}.product.categories', compact('product', 'categories')); }
public function show(Category $category) { $products = $category->activeProducts()->with(['image'])->paginate(); return view('category::__front.categories.show', compact('category', 'products')); }
/** * Bind data to the view. * * @param View $view * @return void */ public function compose(View $view) { $categories = Category::all()->toHierarchy()->toArray(); $view->with('categories', $categories); }