vendredi 9 août 2019

Laravel - Why I can not override createBlueprint function in database schema builder?

I'm using laravel 5.7 and I want to override createBlueprint function in Illuminate\Database\Schema\Builder. I think it ok but when I run migrate I got an error:

Declaration of App\Helpers\SchemaBuilder::blueprintResolver(App\Helpers\Closure $resolver) should be compatible with Illuminate\Database\Schema\Builder::blueprintResolver(Closure $resolver)

Help me override this function, many thanks.

<?php
namespace App\Helpers;

use Illuminate\Database\Schema\Builder as BaseBuilder;
use App\Helpers\Blueprint as XBlueprint;

class SchemaBuilder extends BaseBuilder
{
    /**
     * Create a new command set with a Closure.
     *
     * @param  string  $table
     * @param  \Closure|null  $callback
     * @return \App\Helpers\Blueprint
     */
    protected function createBlueprint($table, Closure $callback = null)
    {
        $prefix = $this->connection->getConfig('prefix_indexes')
                    ? $this->connection->getConfig('prefix')
                    : '';

        if (isset($this->resolver)) {
            return call_user_func($this->resolver, $table, $callback, $prefix);
        }

        return new XBlueprint($table, $callback, $prefix);
    }

    /**
     * Set the Schema Blueprint resolver callback.
     *
     * @param  \Closure  $resolver
     * @return void
     */
    public function blueprintResolver(Closure $resolver)
    {
        $this->resolver = $resolver;
    }
}

<?php

namespace App\Helpers;

use Illuminate\Database\Schema\Blueprint as BaseBlueprint;

class Blueprint extends BaseBlueprint
{
    /**
     * Indicate that the given columns should be dropped.
     *
     * @param  array|mixed  $columns
     * @return \Illuminate\Support\Fluent
     */
    public function dropColumn($columns)
    {
        $columns = is_array($columns) ? $columns : func_get_args();

        foreach ($columns as $value) {
            $this->droppedColumns[] = [
                'type' => '',
                'name' => $value,
                'length' => '',
                'drop' => true,
            ];
        }

        return $this->addCommand('dropColumn', compact('columns'));
    }

    /**
     * Get the columns on the blueprint that should be dropped.
     *
     * @return \Illuminate\Database\Schema\ColumnDefinition[]
     */
    public function getDroppedColumns()
    {
        return $this->droppedColumns;
    }

    /**
     * Get the all columns on the blueprint contains added, drop, change.
     *
     * @return \Illuminate\Database\Schema\ColumnDefinition[]
     */
    public function getAllColumns()
    {
        return array_merge($this->getColumns(), $this->getDroppedColumns());
    }
}



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire