我尝试将数据播种到数据库中的表时出错。我有2个表:作者和书籍。该表具有使用ORM的关系。但是,当我尝试使用
执行它时php artisan migrate:refresh --seed
我收到了这个错误:
无法找到四位数 数据丢失。
这是我的代码
我的模特“作者”:
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Author extends Model
{
protected $fillable = ['name'];
public function books(){
return $this->hasMany('App\Book');
}
}
模型“书籍”:
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Book extends Model
{
protected $fillable = ['name','author_id','amount'];
public function author(){
return $this->belongsTo('App\Author');
}
}
我的播种代码:
<?php
use Illuminate\Database\Seeder;
use App\Author;
use App\Book;
class BooksSeeder extends Seeder
{
/**
* Run the database seeds.
*
* @return void
*/
public function run()
{
$author1 = Author::create(['Muhammad Ariel Purnama']);
$author2 = Author::create(['Titania Eka Yuliandini']);
$author3 = Author::create(['Shikaku kookokokoko']);
$book1 = Book::create(['title' => 'goood boy', 'amount' => 3, 'author_id' => $author1->id]);
$book2 = Book::create(['title' => 'life observer', 'amount' => 4,'author_id' => $author2->id]);
$book3 = Book::create(['title' => 'Blekok Borokokko', 'amount' => 2, 'author_id' => $author3->id]);
}
}
和我的DatabaseSeeder代码:
<?php
use Illuminate\Database\Seeder;
class DatabaseSeeder extends Seeder
{
/**
* Run the database seeds.
*
* @return void
*/
public function run()
{
$this->call(PostsTableSeeder::class);
$this->call(UsersSeeder::class);
$this->call(BooksSeeder::class);
}
}
谢谢你的关注。
答案 0 :(得分:0)
使用时应使用正确的字段名称,并输入正确的密钥:
<preference name="KeyboardDisplayRequiresUserAction" value="false"/>
仔细检查您的图书表格列是$author1 = Author::create(['name'=>'Muhammad Ariel Purnama']); //use name key
$author2 = Author::create(['name'=>'Titania Eka Yuliandini']); //use name key
$author3 = Author::create(['name'=>'Shikaku kookokokoko']); //use name key
$book1 = Book::create(['name' => 'goood boy', 'amount' => 3, 'author_id' => $author1->id]); //replace title for name
$book2 = Book::create(['name' => 'life observer', 'amount' => 4,'author_id' => $author2->id]); //replace title for name
$book3 = Book::create(['name' => 'Blekok Borokokko', 'amount' => 2, 'author_id' => $author3->id]); //replace title for name
还是title
,然后在模型和种子中进行调整。