database/seeds/BookCategoryRelationsTableSeeder.php
/**
     * Run the database seeds.
     *
     * @return void
     */
Run the database seeds.
@return void
Run the database seeds . @return void
public function run()
    {
        DB::table('book_category_relations')->truncate();
        foreach ($this->mock as $mock) {
            $bookId = Book::query()->where('isbn', $mock['isbn'])->get(['id'])->toArray()[0]['id'];
            foreach ($mock['categories'] as $category) {
                $categoryId = Category::query()->where('name', $category)->get(['id'])->toArray()[0]['id'];
                DB::table('book_category_relations')->insert([
                    'book_id' => $bookId,
                    'category_id' => $categoryId
                ]);
            }
        }
    }
public function run ( ) { DB :: table ( 'book_category_relations' ) -> truncate ( ) ; foreach ( $ this -> mock as $ mock ) { $ bookId = Book :: query ( ) -> where ( 'isbn' , $ mock [ 'isbn' ] ) -> get ( [ 'id' ] ) -> toArray ( ) [ 0 ] [ 'id' ] ; foreach ( $ mock [ 'categories' ] as $ category ) { $ categoryId = Category :: query ( ) -> where ( 'name' , $ category ) -> get ( [ 'id' ] ) -> toArray ( ) [ 0 ] [ 'id' ] ; DB :: table ( 'book_category_relations' ) -> insert ( [ 'book_id' => $ bookId , 'category_id' => $ categoryId ] ) ; } } }
Run the database seeds.
Run the database seeds .