0

I'm new to Laravel Framework and php I started doing basic Laravel project and got his error message while trying to make migrations(and without changing anything in the file just open it and the error was shown): Avoid using static access to class '\Illuminate\Support\Facades\Schema' in method 'up'

My code:

<?php

namespace App\Providers;

use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
use Illuminate\Support\ServiceProvider;


class CreatePostsTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('posts', function (Blueprint $table) {
            $table->increments('id');
            $table->mediumText('body');
            $table->timestamps();

        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::dropIfExists('posts');
    }
}

I got the same error for every method in every migration class.

database.php:

 'mysql' => [
            'driver' => 'mysql',
            'host' => env('DB_HOST', '127.0.0.1'),
            'port' => env('DB_PORT', '3306'),
            'database' => env('DB_DATABASE', 'llsapp'),
            'username' => env('DB_USERNAME', 'root'),
            'password' => env('DB_PASSWORD', '123456'),
            'unix_socket' => env('DB_SOCKET', ''),
            'charset' => 'utf8mb4',
            'collation' => 'utf8mb4_unicode_ci',
            'prefix' => '',
            'strict' => true,
            'engine' => null,
        ],

.env:

APP_NAME=LLSAPP
APP_ENV=local
APP_KEY=
APP_DEBUG=true
APP_LOG_LEVEL=debug
APP_URL=http://localhost

DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=llsapp
DB_USERNAME="root"
DB_PASSWORD="123456"

BROADCAST_DRIVER=log
CACHE_DRIVER=file
SESSION_DRIVER=file
QUEUE_DRIVER=sync

REDIS_HOST=127.0.0.1
REDIS_PASSWORD=null
REDIS_PORT=6379

MAIL_DRIVER=smtp
MAIL_HOST=smtp.mailtrap.io
MAIL_PORT=2525
MAIL_USERNAME=null
MAIL_PASSWORD=null
MAIL_ENCRYPTION=null

PUSHER_APP_ID=
PUSHER_APP_KEY=
PUSHER_APP_SECRET=
  • Try with `use Schema;` – STA Jul 13 '20 at 13:06
  • Hi. It looks like you have this code in the wrong place. This should not be namespaced App\Providers and it should not `use` ServiceProvider class. To create a migration go to the command line (maybe best in homestead) and run `php artisan make:migration create_post_table --create='posts' ` . Then run that migration via `php artisan migrate` command line. – Tarek Adam Jul 13 '20 at 13:09
  • Forgot to mention, the migration file will appear in your /database/migrations/... – Tarek Adam Jul 13 '20 at 13:12
  • use Schema didn't work for me. I remove this 2 lines use ServiceProvider and the namespace but still get the same error and after running the php artisan migrate I got : SQLSTATE[HY000] [1045] Access denied for user 'root'@'localhost' (using password: YES) (SQL: se lect * from information_schema.tables where table_schema = llsapp and table_name = migrations) In Connector.php line 68: SQLSTATE[HY000] [1045] Access denied for user 'root'@'localhost' (using password: YES) –  Jul 13 '20 at 13:38
  • mysql.server start on terminal? – Sergio Jul 13 '20 at 13:51
  • Yes, I restart the server but nothing changed. PS: I edited the post with config/database.php file and .env file. I'm using phpMyAdmin and xampp –  Jul 13 '20 at 14:26
  • @STA: please explain why using that Schema class could help – Nico Haase Jul 14 '20 at 07:49
  • @Nico I faced the same problem, after `use Schema`, it has gone . https://stackoverflow.com/a/50579655/4575350 If I have explanation then I would give an answer instead of comment. – STA Jul 14 '20 at 08:01
  • @STA: the error message from your linked question is completely different - if the Schema class was not found, there was no way that any tool would tell you to avoid a static access – Nico Haase Jul 14 '20 at 08:03

1 Answers1

0

I found the solution: uninstall the extension PHP Mess Detector(phpmd), then delete all of the migrations and files that created and create them again. This worked for me perfectly.