The error message suggests that there is an unknown column in the field list of the insert query. Looking at the seeder code, it seems that the issue is with the attach method in the pivot table relationship.
The attach method expects an array of IDs as the first argument, but in the code provided, it is passing a collection of contacts. To fix this, you can use the pluck method on the contacts collection to get an array of contact IDs.
Here's the updated seeder code:
public function run(): void
{
$contacts = Contact::all();
Vehicle::factory(100)->create()->each(function($vehicle) use($contacts) {
$vehicle->contacts()->attach($contacts->pluck('id')->random(3), [
'takeover_id' => Takeover::pluck('id')->random(),
'accessory_id' => Accessory::pluck('id')->random(),
'status_id' => Status::pluck('id')->random(),
'edited_by' => User::pluck('id')->random(),
'created_by' => User::pluck('id')->random(),
'total_price' => faker()->randomFloat(2, 1000, 90000),
'discount' => faker()->randomFloat(2, 1000, 5000),
]);
});
}
In the updated code, the pluck method is used to get an array of contact IDs, and then the random method is called on the array to select three random contact IDs.
Make sure to import the necessary classes at the top of the seeder file:
use App\Models\Contact;
use App\Models\Vehicle;
use App\Models\Takeover;
use App\Models\Accessory;
use App\Models\Status;
use App\Models\User;
use Illuminate\Support\Facades\DB;
use Illuminate\Database\Seeder;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Str;
Remember to replace the App\Models namespace with the correct namespace for your models.
This should resolve the issue with the unknown column in the field list error.