使用PHP Faker填充数据报错:Illuminate\Database\Query\Builder::issues不存在
Hey there, let’s break down what’s causing this error and how to fix it. That message means somewhere in your gamesSeeder code, you’re trying to call an issues() method directly on a query builder instance (like the one you get from DB::table('games')) instead of on a model that has that relationship defined. Here are the most likely fixes:
1. Correct method calls in your gamesSeeder
First, check your seeder code for lines that look like this:
DB::table('games')->issues()->create([...]);
This is the problem—DB::table() returns a query builder, which doesn’t have an issues() method. Instead, use your App\games model and call the relationship on an actual model instance:
// Create a game model instance first $game = App\games::factory()->create(); // Then call the issues relationship on the instance $game->issues()->create([...]);
2. Define the issues relationship in your App\games model
If you’re trying to link games to issues via a relationship, you need to add the method to your model. For example, if each game has many issues:
<?php namespace App; use Illuminate\Database\Eloquent\Model; class games extends Model { // ... other model code ... public function issues() { // Adjust the related model and foreign key to match your schema return $this->hasMany(App\Issue::class, 'game_id'); } }
Make sure the related model (like App\Issue) exists and the foreign key matches your database setup.
3. Check for typos in method names
It’s easy to mistype a method name—double-check if you meant to call something else (like where() or get()) but accidentally wrote issues(). For example, if you had a typo like:
DB::table('games')->issues('status', '=', 'active')->get();
That should be corrected to:
DB::table('games')->where('status', '=', 'active')->get();
4. Verify model imports in your seeder
Make sure you’ve imported the App\games model at the top of your gamesSeeder.php file:
<?php use App\games; use Illuminate\Database\Seeder; class gamesSeeder extends Seeder { // ... seeder code ... }
This ensures you’re using the model class (which can have relationships) instead of raw database queries.
Once you fix these, running php artisan db:seed should work without that error.
内容的提问来源于stack exchange,提问作者Hanna

