我创建了一个StudentTableSeeder来为学生表添加种子,同时为每个学生播种时,我想生成一个学生的图书馆和费用证明,无论何时由StudentTableSeeder创建一个学生。我收到此错误“ FatalThrowableError:函数LibraryClearanceTableSeeder :: run()的参数太少,传递了0个且恰好期望有1个”
这是StudentTableSeeder代码
use App\Models\Student;
use Illuminate\Database\Seeder;
class StudentsTableSeeder extends Seeder
{
protected $students = [
[
'id' => 1,
'firstname' => 'Emmanuel',
'middlename' => 'kwame',
'lastname' => 'Agyapong',
'index_no' => 'ANU16280111',
'nationality' => 0,
'regular_or_weekend' => 1,
'image' => null,
'course_id' => 1,
],
];
public function run()
{
foreach ($this->students as $index => $student)
{
$result = Student::create($student);
if (!$result) {
$this->command->info("Insert failed at record $index.");
return;
}
$this->call(LibraryClearanceTableSeeder::class)->run($students['student_id']);
}
$this->command->info('Inserted '.count($this->students). ' records');
}
}
这是我的LibraryClearanceTableSeeder
use App\Models\Library;
use Illuminate\Database\Seeder;
use Illuminate\Support\Facades\DB;
class LibraryClearanceTableSeeder extends Seeder
{
public function run($studentid)
{
DB::table('libraries')->create([
'student_id' => $studentid,
'borrowed' => 0,
]);
}
}
答案 0 :(得分:1)
在播种器中使用静态功能
这是StudentTableSeeder代码
use App\Models\Student;
use Illuminate\Database\Seeder;
class StudentsTableSeeder extends Seeder
{
protected $students = [
[
'id' => 1,
'firstname' => 'Emmanuel',
'middlename' => 'kwame',
'lastname' => 'Agyapong',
'index_no' => 'ANU16280111',
'nationality' => 0,
'regular_or_weekend' => 1,
'image' => null,
'course_id' => 1,
],
];
public function run()
{
foreach ($this->students as $index => $student)
{
$result = Student::create($student);
if (!$result) {
$this->command->info("Insert failed at record $index.");
return;
}
LibraryClearanceTableSeeder::setLib($student['id']);
}
$this->command->info('Inserted '.count($this->students). ' records');
}
}
这是LibraryClearanceTableSeeder
use App\Models\Library;
use Illuminate\Database\Seeder;
use Illuminate\Support\Facades\DB;
class LibraryClearanceTableSeeder extends Seeder
{
public function run()
{
}
public static function setLib($studentid)
{
DB::table('libraries')->create([
'student_id' => $studentid,
'borrowed' => 0,
]);
}
}
答案 1 :(得分:0)
我正在获取badmethodcallexception,所以我在LibraryClearanceTableSeeder中将create()更改为insert(),并且有效。
public static function setLib($studentid)
{
DB::table('libraries')->insert([
'student_id' => $studentid,
'borrowed' => 0,
]);
}