【问题标题】:Laravel get Error: Call to undefined function table()!Laravel 报错:调用未定义的函数表()!
【发布时间】:2018-02-26 06:47:43
【问题描述】:

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

请帮我解决。

谢谢。

【问题讨论】:

  • DB 类的use 语句在哪里?
  • 我添加了使用 Illuminate\Support\Facades\DB;到这 3 个文件。但仍然无法正常工作并出现相同的错误
  • 你能显示整个 Laravel 错误吗?错误中可能包含更多详细信息,可以帮助您解决该问题。
  • 播种:GroupTableSeeder [Symfony\Component\Debug\Exception\FatalThrowableError] 调用未定义的函数 table() 这就是 cmd 中显示的所有错误

标签: php laravel web


【解决方案1】:

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

use Illuminate\Support\Facades\DB;

到您使用的所有文件DB

【讨论】:

  • 我添加了使用 Illuminate\Support\Facades\DB;到这 3 个文件。但仍然无法正常工作并出现相同的错误
  • @rezaasghari 你跑过composer dump-autoload吗?
【解决方案2】:

你必须导入数据库:

use DB;

或者加斜线

\DB::table(...)

【讨论】:

    【解决方案3】:

    在设置它进行测试时,我注意到存在语法错误:

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

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

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

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

    【讨论】:

    • 顺便说一句,对于那些说要输入 \DB 或类似内容的人来说,播种机位于根命名空间中,因此可以直接引用 DB 外观而没有 \ 并且没有包括use DB;
    • 是的,你是对的@james-funk。我有一个语法错误。问题已经解决了。非常感谢
    【解决方案4】:
    <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>
    

    【讨论】:

    • 当你添加代码时尝试格式化它,看起来会更好
    猜你喜欢
    • 1970-01-01
    • 2016-10-03
    • 2016-03-30
    • 1970-01-01
    • 2023-03-14
    • 2019-06-14
    • 2016-08-05
    • 1970-01-01
    • 2020-12-03
    相关资源
    最近更新 更多