Now we will move on to products. In this lesson, we will create a table of Products, powered by Livewire. For now, we will just show a list of products, and in other lessons, we will add features to it.

First, we will create a Livewire component...
Now we will move on to products. In this lesson, we will create a table of Products, powered by Livewire. For now, we will just show a list of products, and in other lessons, we will add features to it.

First, we will create a Livewire component...
To seed database in this step, open CategoryFactory.php and amend:
public function definition(): array { $city = fake()->unique()->city(); return [ 'name' => $city, 'slug' => Str::slug($city), 'position' => fake()->numberBetween(1, 10), ]; }
Create ProductFactory.php and amend:
public function definition(): array { return [ 'name' => fake()->catchPhrase(), 'description' => fake()->realText(), 'country_id' => fake()->numberBetween(1, 240), 'price' => fake()->numberBetween(100, 500), ]; }
And amend in DatabaseSeeder.php
public function run(): void { User::factory(10)->create(); $this->call([ CountriesSeeder::class, ]); Product::factory(10)->create()->each(function ($product) { $product->categories() ->saveMany(Category::factory(mt_rand(1, 2))->make()); }); }
We are seeding products with belongs to many relationship. Execute to seed the data:
php artisan migrate:fresh --seed
Correct me please, if I'm wrong but It generates a unique category/categories for each product, right?
Product::factory(10)->create()->each(function ($product) { $product->categories() ->saveMany(Category::factory(mt_rand(1, 2))->make());});
my approach is :
// ... Category::factory()->count(11)->create(); $categories = Category::all(); Product::factory()->count(11)->create()->each( function($product) use ($categories) { $product->categories()->attach($categories->random(random_int(1,3))); } );
Dear @Povilas Korop,
I'm afraid there are typos in your code. Shouldn't we type hint the relationship in pascal case like this?
use Illuminate\Database\Eloquent\Relations\BelongsToMany; public function categories(): BelongsToMany { return $this->belongsToMany(Category::class); }
Hello,
I have installed the code from the repository (laravel 10.45 but I encounter the same problem with laravel 11) and migrated/seeded the database on a brand new one.
On a listing page (products or orders), I have found a weird behavior concerning the checkboxes on the left when going from one page to another using the pagination below the table.
If I check some checkboxes on the first page for example and then go to the second page (or any other), the same rows are checked on the second page and if I uncheck one of them on the second page, all become unchecked.
Looking forward for a solution
when I was trying to give product model command this error is given ErrorException
include(C:\Users\HP\Documents\Project\project_one\vendor\composer/../../app/Http/Livewire/ProductList.php): Failed to open s stream: No such file or directory
at C:\Users\HP\Documents\Project\project_one\vendor\composer\ClassLoader.php:578 574▕ * @param string $file 575▕ * @return void 576▕ */ 577▕ self::$includeFile = static function($file) { ➜ 578▕ include $file; 579▕ }; 580▕ } 581▕ } 582▕
1 C:\Users\HP\Documents\Project\project_one\vendor\composer\ClassLoader.php:578 include()
2 C:\Users\HP\Documents\Project\project_one\vendor\composer\ClassLoader.php:432 Composer\Autoload\ClassLoader::Composer\Autoload{closure}("C:\Users\HP\Documents\Project\project_one\vendor\composer/.../../app/Http/Livewire/ProductList.php")
What do you mean by "give product model command"? It seems like the file of ProductList.php is not found, can you double check it actually exists? Sorry, I can't really debug it for you, the error message is pretty clear, I won't help you more than that.
sorry I figure it out bug was because I did not give the data of product country in right sequence in database migration thats why i could not migrate it