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

时间:2017-09-17 18:08:19

标签: php laravel web

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

请帮我解决一下。

感谢。

4 个答案:

答案 0 :(得分:1)

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

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

在那里调用::时应该有两个DB::table。这可能是对SO错误的复制/粘贴,但我认为这是问题的原因。我添加了正确的::和我的播种机(我复制/粘贴你)运行正常。我将其更改为单个:并再次尝试,我收到错误:

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

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

答案 1 :(得分: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>

答案 2 :(得分:0)

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

State 88

   49 expr: expr . "[" expr "]"
   50        | expr . '&' expr
   51        | expr . '*' expr
   52        | expr . '!' expr
   53        | expr . INCR
   54        | expr . DCRS
   55        | expr . '=' expr
   55        | expr '=' expr .  ['!', '&', '*', LE, GE, EQ, NE, LT, GT, MUL_AS, DIV_AS, ADD_AS, MIN_AS, MOD_AS, INCR, DCRS, ";", "[", "]", ")", ')', ';', '=', "?", ":", ',']
   56        | expr . DIV_AS expr
   57        | expr . MOD_AS expr
   58        | expr . MUL_AS expr
   59        | expr . ADD_AS expr
   60        | expr . MIN_AS expr
   61        | expr . LE expr
   62        | expr . GE expr
   63        | expr . NE expr
   64        | expr . EQ expr
   65        | expr . GT expr
   66        | expr . LT expr
   68        | expr . "?" expr ":" expr

    '!'     shift, and go to state 44
    '&'     shift, and go to state 45
    '*'     shift, and go to state 46
    LE      shift, and go to state 47
    GE      shift, and go to state 48
    EQ      shift, and go to state 49
    NE      shift, and go to state 50
    LT      shift, and go to state 51
    GT      shift, and go to state 52
    MUL_AS  shift, and go to state 53
    DIV_AS  shift, and go to state 54
    ADD_AS  shift, and go to state 55
    MIN_AS  shift, and go to state 56
    MOD_AS  shift, and go to state 57
    INCR    shift, and go to state 58
    DCRS    shift, and go to state 59
    "["     shift, and go to state 60
    '='     shift, and go to state 61
    "?"     shift, and go to state 62

    '!'       [reduce using rule 55 (expr)]
    '&'       [reduce using rule 55 (expr)]
    '*'       [reduce using rule 55 (expr)]
    LE        [reduce using rule 55 (expr)]
    GE        [reduce using rule 55 (expr)]
    EQ        [reduce using rule 55 (expr)]
    NE        [reduce using rule 55 (expr)]
    LT        [reduce using rule 55 (expr)]
    GT        [reduce using rule 55 (expr)]
    MUL_AS    [reduce using rule 55 (expr)]
    DIV_AS    [reduce using rule 55 (expr)]
    ADD_AS    [reduce using rule 55 (expr)]
    MIN_AS    [reduce using rule 55 (expr)]
    MOD_AS    [reduce using rule 55 (expr)]
    INCR      [reduce using rule 55 (expr)]
    DCRS      [reduce using rule 55 (expr)]
    "["       [reduce using rule 55 (expr)]
    '='       [reduce using rule 55 (expr)]
    "?"       [reduce using rule 55 (expr)]
    $default  reduce using rule 55 (expr)

到您使用use Illuminate\Support\Facades\DB;

的所有文件

答案 3 :(得分:0)

您必须导入数据库:

use DB;

或添加斜杠

\DB::table(...)