简体   繁体   中英

Is it possible add tinyInteger or smallInteger to increments on laravel ORM?

Is possible to add that code or something like that to laravel\\Illuminate\\Database\\Schema\\Blueprint to use with migrations?

public function incrementsTiny($column)
{
   return $this->unsignedTinyInteger($column, true);
}

public function incrementsSmall($column)
{
   return $this->unsignedSmallInteger($column, true);
}

scenario: some temp table that don't grow high and have some useful information or just small table that do not have more than 100 lines and need some rare update (add or just change). But it is possible to add to the framework? Its common to have a lot information, but sometimes sometables dont have a lot of data.

Because for increments just have the option for integer or bigInteger

您可以使用类似:

$table->tinyInteger('id')->unsigned()->autoIncrement();

Navigate to: laravel/vendor/laravel/framework/src/Illuminate/Database/Schema

Open: Blueprint.php

Find:

public function increments($column)
    {
        return $this->unsignedInteger($column, true);
    }

Add after this:

     /**
     * Create a new auto-incrementing tiny integer column on the table.
     *
     * @param  string  $column
     * @return \Illuminate\Support\Fluent
     */
    public function incrementsTinyInteger($column)
    {
        return $this->unsignedTinyInteger($column, true);
    }

    /**
     * Create a new auto-incrementing small integer column on the table.
     *
     * @param  string  $column
     * @return \Illuminate\Support\Fluent
     */
    public function incrementsSmallInteger($column)
    {
        return $this->unsignedSmallInteger($column, true);
    }

    /**
     * Create a new auto-incrementing medium integer column on the table.
     *
     * @param  string  $column
     * @return \Illuminate\Support\Fluent
     */
    public function incrementsMediumInteger($column)
    {
        return $this->unsignedMediumInteger($column, true);
    }

Find:

public function unsignedInteger($column, $autoIncrement = false)
    {
        return $this->integer($column, $autoIncrement, true);
    }

Add after this:

     /**
     * Create a new unsigned tiny integer column on the table.
     *
     * @param  string  $column
     * @param  bool  $autoIncrement
     * @param  bool  $unsigned
     * @return \Illuminate\Support\Fluent
     */
    public function unsignedTinyInteger($column, $autoIncrement = false)
    {
        return $this->tinyInteger($column, $autoIncrement, true);
    }

    /**
     * Create a new unsigned small integer column on the table.
     *
     * @param  string  $column
     * @param  bool  $autoIncrement
     * @param  bool  $unsigned
     * @return \Illuminate\Support\Fluent
     */
    public function unsignedSmallInteger($column, $autoIncrement = false)
    {
        return $this->smallInteger($column, $autoIncrement, true);
    }

    /**
     * Create a new unsigned medium integer column on the table.
     *
     * @param  string  $column
     * @param  bool  $autoIncrement
     * @param  bool  $unsigned
     * @return \Illuminate\Support\Fluent
     */
    public function unsignedMediumInteger($column, $autoIncrement = false)
    {
        return $this->mediumInteger($column, $autoIncrement, true);
    }

Navigate to: laravel/vendor/laravel/framework/src/Illuminate/Database/Schema/ Grammars

Open: MySqlGrammar.php

Find: protected $serials = array('bigInteger', 'integer');

Change to: protected $serials = array('bigInteger', 'integer', 'tinyInteger', 'smallInteger', 'mediumInteger');

plus, in the same file above, find:

protected function typeTinyInteger(Fluent $column)
    {
        return 'tinyint(1)';
    }

Change to:

protected function typeTinyInteger(Fluent $column)
    {
        return 'tinyint';
    }

If someone know how to extend this files and config the usage in laravel, and want to share the how-to i will apreciate. But I dont know how to config everthing after extend these files and this is the way i know how to do this in laravel.

$table->tinyInteger('id', true, true);

You can see tinyInteger function definition in Illuminate\\Database\\Schema\\Blueprint.php:

/**
 * Create a new tiny integer (1-byte) column on the table.
 *
 * @param  string  $column
 * @param  bool  $autoIncrement
 * @param  bool  $unsigned
 * @return \Illuminate\Support\Fluent
 */
public function tinyInteger($column, $autoIncrement = false, $unsigned = false)
{
    return $this->addColumn('tinyInteger', $column, compact('autoIncrement', 'unsigned'));
}

So you just set the 2nd and 3rd parameters to true and you get unsigned autoIncrement not null primary key.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM