While running this command php artisan db:seed
I get this error: "Call to undefined function Database\Seeders\table()"
Screen shot:
error in db seeding
As I am new to laravel , I coundn't fix the bug of my code after searching for various examples.I created a model by php artisan make:model ServiceCategory -m
command which created this model ServiceCategory.php :
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
class ServiceCategory extends Model
{
use HasFactory;
}
and also created a migration 2022_07_20_200919_create_service_categories_table.php file :
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
return new class extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('service_categories', function (Blueprint $table) {
$table->id();
$table->string('name');
$table->string('slug')->index();
$table->string('image');
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('service_categories');
}
};
Then I created a seeder name : ServiceCategorySeeder.php :
<?php
namespace Database\Seeders;
// use Illuminate\Database\Console\Seeds\WithoutModelEvents;
use Illuminate\Database\Seeder;
class ServiceCategorySeeder extends Seeder
{
public function run()
{
DB:table('service_categories')->insert([
[
"name"=> "AC Installation",
"slug"=>"ac-installation",
"image"=>"ac installation.png"
],
[
"name"=> "AC Uninstallation",
"slug"=>"ac-uninstallation",
"image"=>"ac uninstallation.png"
],
[
"name"=> "AC Repair",
"slug"=>"ac-repair",
"image"=>"ac repair.png"
],
[
"name"=> "Laundry",
"slug"=>"laundry",
"image"=>"laundry.png"
],
[
"name"=> "Electrical",
"slug"=>"electrical",
"image"=>"electrical.png"
],
[
"name"=> "Plumbing",
"slug"=>"plumbing",
"image"=>"plumbing.png"
],
[
"name"=> "Painting",
"slug"=>"painting",
"image"=>"painting.png"
],
[
"name"=> "House Shifitng",
"slug"=>"house shifitng",
"image"=>"house-shifitng.png"
],
[
"name"=> "Tank Cleaning",
"slug"=>"tank cleaning",
"image"=>"tank-cleaning.png"
],
[
"name"=> "Furniture",
"slug"=>"furniture",
"image"=>"furniture.png"
],
[
"name"=> "Home Deep Cleaning",
"slug"=>"home-deep-cleaning",
"image"=>"home deep cleaning.png"
],
[
"name"=> "Bathroom Deep Cleaning",
"slug"=>"bathroom-deep-cleaning",
"image"=>"bathroom deep cleaning.png"
]
]);
}
}
also modified DatabaseSeeder.php is:
<?php
namespace Database\Seeders;
// use Illuminate\Database\Console\Seeds\WithoutModelEvents;
use Illuminate\Database\Seeder;
class DatabaseSeeder extends Seeder
{
/**
* Seed the application's database.
*
* @return void
*/
public function run()
{
$this->call([
ServiceCategorySeeder::class
]);
}
}
The successfully running the command : composer dump-autoload
I ran the command php artisan db:seed
which lead the this error is ss:
ABIR HASSAN@LAPTOP-D9T7UM30 MINGW64 /d/XAMP/htdocs/testproject_Copy
$ php artisan db:seed
Seeding: Database\Seeders\ServiceCategorySeeder
Error
Call to undefined function Database\Seeders\table()
at D:\XAMP\htdocs\testproject_Copy\database\seeders\ServiceCategorySeeder.php:13
9▕ {
10▕
11▕ public function run()
12▕ {
➜ 13▕ DB:table('service_categories')->insert([
14▕ [
15▕ "name"=> "AC Installation",
16▕ "slug"=>"ac-installation",
17▕ "image"=>"ac installation.png"
1 D:\XAMP\htdocs\testproject_Copy\vendor\laravel\framework\src\Illuminate\Container\BoundMethod.php:36
Database\Seeders\ServiceCategorySeeder::run()
2 D:\XAMP\htdocs\testproject_Copy\vendor\laravel\framework\src\Illuminate\Container\Util.php:41
Illuminate\Container\BoundMethod::Illuminate\Container\{closure}()
Thank you advance for your help.
CodePudding user response:
In your seeder, you need DB::table
instead of DB:table