Laravel Coverage Does Not Fill Fields

I have a database sample file:

class ContactTableSeeder extends Seeder { public function run() { $contacts = array( array( 'first_name' => 'Test', 'last_name' => 'Contact', 'email' => ' test.contact@emai.com ', 'telephone_number' => '0111345685', 'address' => 'Address', 'city' => 'City', 'postcode' => 'postcode', 'position' => 'Director', 'account_id' => 1 ) ); foreach ($contacts as $contact) { Contact::create($contact); } } } 

When I run php artisan migrate: refresh --seed, it pops up the database and creates the corresponding entry in the contact table, except that it does not fill out the fields with any information in the seed array. I use the same syntax for other tables and they work fine, and I also carefully checked each field to make sure they match the fields of the database, but no matter what I do, it will not be the correct seed.

Does anyone have any ideas?

+4
source share
4 answers

It turns out the problem is with relationships in my models.

For future visitors to this question: be sure to check all the functions in the models that define the hasOne / hasMany / etc relationship. Read the Vivid Documents for more details.

+1
source

I had the same problem, but none of the solutions above worked for me. It turned out that my model has a build function! After I removed it, it worked fine!

 public function __construct() { parent::__construct(); } 

EDIT: After further reading on this subject, I found that the problem is that if you are going to include a constructor in your model, you must accept the attribute parameter and pass it to the parent. If you do this, the constructor will not break the sowing of the database (and possibly other things). Hope this saves someone else a headache.

 public function __construct($attributes = array()) { parent::__construct($attributes); } 
+7
source

You tried to replace the following lines:

 foreach ($contacts as $contact) { Contact::create($contact); } 

with

 DB::table('contact')->insert($contacts); 

Assuming your table name is a contact. Also, make sure you have a line like this

 $this->call('ContactTableSeeder'); 

in your DatabaseSeeder class.

0
source

Do you have $this->call("ContactTableSeeder") in your DatabaseSeeder ' run() function?

If you have ContactTableSeeder in your own file, is there a file called ContactTableSeeder.php sure? If not, it will not load in accordance with the PSR-0 standard .

These are my first thoughts.

0
source

Source: https://habr.com/ru/post/1480236/


All Articles