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

好久不见. 提交于 2020-01-02 06:15:10

问题


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


回答1:


You can use something like:

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



回答2:


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.




回答3:


$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.



来源:https://stackoverflow.com/questions/19711783/is-it-possible-add-tinyinteger-or-smallinteger-to-increments-on-laravel-orm

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!