Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Can not create Migration file with Laravel 9, with Php 8.0.1 #36

Open
N30 opened this issue May 20, 2022 · 4 comments
Open

Can not create Migration file with Laravel 9, with Php 8.0.1 #36

N30 opened this issue May 20, 2022 · 4 comments

Comments

@N30
Copy link

N30 commented May 20, 2022

Can not create Migration file with Laravel 9, with Php 8.0.1

Declaration of Encore\Admin\Helpers\Scaffold\MigrationCreator::populateStub($name, $stub, $table) must be compatible with Illuminate\Database\Migrations\MigrationCreator::populateStub($stub, $table) in file C:\Users\colum\Documents\Company\MarijuanaData\marijuanadata.com\vendor\laravel-admin-ext\helpers\src\Scaffold\MigrationCreator.php on line 55

@KulwinderKohli
Copy link

Hello @N30,
I got the same problem and solved it by making changes to Encore\Admin\Helpers\Scaffold\MigrationCreator class file.

Please copy and replace the below code to Encore\Admin\Helpers\Scaffold\MigrationCreator class file.

<?php

namespace Encore\Admin\Helpers\Scaffold;

use Illuminate\Database\Migrations\MigrationCreator as BaseMigrationCreator;
use Illuminate\Filesystem\Filesystem;
use Illuminate\Support\Arr;

class MigrationCreator extends BaseMigrationCreator
{
    /**
     * @var string
     */
    protected $bluePrint = '';
    
    public $name;

    public function __construct(Filesystem $files, $customStubPath = null)
    {
        parent::__construct($files, $customStubPath);
    }

    /**
     * Create a new model.
     *
     * @param string    $name
     * @param string    $path
     * @param null      $table
     * @param bool|true $create
     *
     * @return string
     */
    public function create($name, $path, $table = null, $create = true)
    {
        $this->name = $name;

        $this->ensureMigrationDoesntAlreadyExist($name);

        $path = $this->getPath($name, $path);

        $stub = $this->files->get(__DIR__.'/stubs/create.stub');

        $this->files->put($path, $this->populateStub($stub, $table));

        $this->firePostCreateHooks($table, $path);
        

        return $path;
    }

    /**
     * Populate stub.
     *
     * @param string $name
     * @param string $stub
     * @param string $table
     *
     * @return mixed
     */
    protected function populateStub($stub, $table)
    {
        // Here we will replace the table place-holders with the table specified by
        // the developer, which is useful for quickly creating a tables creation
        // or update migration from the console instead of typing it manually.
        if (! is_null($table)) {
            $stub = str_replace(
                ['DummyClass', 'DummyTable', 'DummyStructure'],
                [$this->getClassName($this->name), $table, $this->bluePrint],
                $stub
            );
        }

        return $stub;
    }

    /**
     * Build the table blueprint.
     *
     * @param array      $fields
     * @param string     $keyName
     * @param bool|true  $useTimestamps
     * @param bool|false $softDeletes
     *
     * @throws \Exception
     *
     * @return $this
     */
    public function buildBluePrint($fields = [], $keyName = 'id', $useTimestamps = true, $softDeletes = false)
    {
        $fields = array_filter($fields, function ($field) {
            return isset($field['name']) && !empty($field['name']);
        });

        if (empty($fields)) {
            throw new \Exception('Table fields can\'t be empty');
        }

        $rows[] = "\$table->increments('$keyName');\n";

        foreach ($fields as $field) {
            $column = "\$table->{$field['type']}('{$field['name']}')";

            if ($field['key']) {
                $column .= "->{$field['key']}()";
            }

            if (isset($field['default']) && $field['default']) {
                $column .= "->default('{$field['default']}')";
            }

            if (isset($field['comment']) && $field['comment']) {
                $column .= "->comment('{$field['comment']}')";
            }

            if (Arr::get($field, 'nullable') == 'on') {
                $column .= '->nullable()';
            }

            $rows[] = $column.";\n";
        }

        if ($useTimestamps) {
            $rows[] = "\$table->timestamps();\n";
        }

        if ($softDeletes) {
            $rows[] = "\$table->softDeletes();\n";
        }

        $this->bluePrint = trim(implode(str_repeat(' ', 12), $rows), "\n");

        return $this;
    }
}

@superwen
Copy link

sure, it works, thank you very much for helping me kill the problem.

@andreabia
Copy link

thankyou KulwinderKohli

@N30
Copy link
Author

N30 commented Jan 23, 2023

Thanks @KulwinderKohli I didn't see this until now. I don't remember what I did, I believe I actually installed awhile new Laravel instance and moved my files hoping a minor version upgrade might fix it. I no longer had the problem. But in generally I never like to change Vendor files, specially Laravel's own... unless it is the last resort

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants