Слишком мало аргументов для функции LibraryClearanceTableSeeder - PullRequest
0 голосов
/ 11 октября 2019

Я создал 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,
    ]);
  }
}

Ответы [ 2 ]

1 голос
/ 11 октября 2019

использовать статическую функцию в сеялке

Это код 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,
    ]);
  }
}
0 голосов
/ 11 октября 2019

я получал badmethodcallexception, поэтому я изменил create () на insert () в LibraryClearanceTableSeeder, и это сработало.

 public static function setLib($studentid)
 {
   DB::table('libraries')->insert([
    'student_id' => $studentid,
    'borrowed'  => 0,
  ]);
}
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...