简体   繁体   中英

Laravel get Error: Call to undefined function table()!

I am new in laravel. whene I want to run seed by this command: php artisan db:seed, I get this error:

[Symfony\\Component\\Debug\\Exception\\FatalThrowableError] Call to undefined function table()

my two seeder class:

1- GroupTableSeeder

    <?php

    use Illuminate\Database\Seeder;
    use Illuminate\Database\Eloquent\Model;

    class GroupTableSeeder extends Seeder
    {
        /**
         * Run the database seeds.
         *
         * @return void
         */
        public function run()
        {
          DB::statement('SET FOREIGN_KEY_CHECKS=0');
          DB::table('groups')->truncate();

          $groups = [
            ['id' => 1, 'name' => 'Family',    'created_at' => new DateTime,         'updated_at' => new DateTime ],
            ['id' => 2, 'name' => 'Friends',   'created_at' => new DateTime, 'updated_at' => new DateTime ],
            ['id' => 3, 'name' => 'Customers', 'created_at' => new DateTime, 'updated_at' => new DateTime ],
            ['id' => 4, 'name' => 'CoWorkers', 'created_at' => new DateTime, 'updated_at' => new DateTime ],
            ['id' => 5, 'name' => 'Teachers',  'created_at' => new DateTime, 'updated_at' => new DateTime ]
          ];

          DB:table('groups')->insert($groups);
        }
    }

2- ContactsTableSeeder

    <?php

    use Illuminate\Database\Seeder;
    use Illuminate\Database\Eloquent\Model;
    use Faker\Factory as Faker;

    class ContactsTableSeeder extends Seeder
    {
        /**
         * Run the database seeds.
         *
         * @return void
         */
        public function run() {

          DB::table( 'contacts' )->truncate();

          $faker = Faker::create();

          $contacts = [];

          foreach ( range( 1, 20 ) as $index ) {

            $contacts[] = [
              'name'       => $faker->name,
              'email'      => $faker->email,
              'phone'      => $faker->phoneNumber,
              'address'    => "{$faker->streetName} {$faker->postcode}                 {$faker->city}",
              'company'    => $faker->company,
              'created_at' => new DateTime,
              'updated_at' => new DateTime,
            ];
          }

          DB::table( 'contacts' )->insert( $contacts );
        }
    }

and also my DatabaseSeeder class:

    <?php

    use Illuminate\Database\Seeder;
    use Illuminate\Database\Eloquent\Model;

    class DatabaseSeeder extends Seeder
    {
        /**
         * Run the database seeds.
         *
         * @return void
         */
        public function run()
        {
            $this->call(GroupTableSeeder::class);
            $this->call(ContactsTableSeeder::class);
        }
    }

please help me to solve it.

Thanks.

When setting this up to test, I noticed that there's a syntax error:

DB:table('contacts')->insert($contacts);

There should be two :: when calling DB::table there. This may be a copy/paste to SO error, but I think it's the cause of the problem. I added the correct :: and my seeders (which i copy/pasted from you) ran fine. I changed it to a single : and tried again, and I got the error:

[Symfony\Component\Debug\Exception\FatalErrorException]  
Call to undefined function table()

Double check that you have the correct syntax there, and it may fix your problem.

<form action="{{ admin_base_path('auth/login') }}" method="post">
      <div class="form-group has-feedback {!! !$errors->has('username') ?: 'has-error' !!}">

        @if($errors->has('username'))
          @foreach($errors->get('username') as $message)
            <label class="control-label" for="inputError"><i class="fa fa-times-circle-o"></i>{{$message}}</label></br>
          @endforeach
        @endif

        <input type="input" class="form-control" placeholder="{{ trans('admin::lang.username') }}" name="username" value="{{ old('username') }}">
        <span class="glyphicon glyphicon-envelope form-control-feedback"></span>
      </div>
      <div class="form-group has-feedback {!! !$errors->has('password') ?: 'has-error' !!}">

        @if($errors->has('password'))
          @foreach($errors->get('password') as $message)
            <label class="control-label" for="inputError"><i class="fa fa-times-circle-o"></i>{{$message}}</label></br>
          @endforeach
        @endif

        <input type="password" class="form-control" placeholder="{{ trans('admin::lang.password') }}" name="password" value="{{ old('username') }}">
        <span class="glyphicon glyphicon-lock form-control-feedback"></span>
      </div>
      <div class="row">

        <!-- /.col -->
        <div class="col-xs-4 col-md-offset-4">
          <input type="hidden" name="_token" value="{{ csrf_token() }}">
          <button type="submit" class="btn btn-primary btn-block btn-flat">{{ trans('admin::lang.login') }}</button>
        </div>
        <!-- /.col -->
      </div>
    </form>

You need to use DB class before using. Add this fully qualified namespace:

use Illuminate\Support\Facades\DB;

to all your files wherever you use DB

You have to import DB:

use DB;

Or add the slash

\DB::table(...)
use Illuminate\Support\Facades\DB;

在行动中: DB::table('')

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM