Laravel得到错误:调用未定义的函数表()!

问题描述 投票:1回答:4

我是laravel的新人。当我想通过这个命令运行种子:php artisan db:seed,我收到此错误:

[Symfony \ Component \ Debug \ Exception \ FatalThrowableError]调用未定义的函数表()

我的两个播种者班:

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 );
        }
    }

还有我的DatabaseSeeder类:

    <?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);
        }
    }

请帮我解决一下。

谢谢。

php laravel web
4个回答
1
投票

将此设置为测试时,我注意到存在语法错误:

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

在那里打电话给::应该有两个DB::table。这可能是SO错误的复制/粘贴,但我认为这是问题的原因。我添加了正确的::和我的播种机(我复制/粘贴你)运行正常。我把它改成单个:并再次尝试,我得到了错误:

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

仔细检查您是否有正确的语法,它可以解决您的问题。


1
投票
<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>

0
投票

使用前需要使用DB类。添加此完全限定名称空间:

use Illuminate\Support\Facades\DB;

无论你在哪里使用DB,你的所有文件


0
投票

你必须导入DB:

use DB;

或者添加斜杠

\DB::table(...)
热门问题
推荐问题
最新问题