当主键是varchar时,无法从Laravel的Eloquent中检索列值

Dav*_*eri 8 php mysql orm laravel eloquent

我遇到了一个问题,我Laravel雄辩的型号不给我所谓的"身份证",它只是变成是一个整数(0),而不是一个字符串列的值.我虽然列以某种方式受到保护,但在其他模型中,'id'是一个整数,它返回值很好.

问题:我不能将VARCHAR用于主键吗?

注意:我必须创建更多具有相应模型的表,其中主键必须是字符串; 我是一个新的Laravel

移民:

Schema::create('country', function (Blueprint $table) {
        $table->engine = 'InnoDB';
        $table->string('id', 5);
        $table->string('name', 45);
        $table->string('currency_symbol', 5);
        $table->string('currency_name', 45);
        $table->float('tax_rate');
        $table->string('url')->nullable();
        $table->string('support_email', 90);
        $table->string('noreply_email', 90);
        $table->boolean('active');
        $table->boolean('root');
        $table->primary('id');
    });
Run Code Online (Sandbox Code Playgroud)

模型非常简单:

use Illuminate\Database\Eloquent\Model;

class Country extends Model
{
    protected $table = 'country';
}
Run Code Online (Sandbox Code Playgroud)

但是当我试图检索它时......

echo Country::find('ve')->first()->toJSON(); 
//Output:
{"id":0,"name":"Test","currency_symbol":"T".....

// And...
var_dump(Country::find('ve')->first()->id);
//Output:
int:0
Run Code Online (Sandbox Code Playgroud)

但是,如果我使用print_r()函数,这是输出:

    App\Http\Models\Country\Country Object
    (
        [table:protected] => country
        [connection:protected] => 
        [primaryKey:protected] => id
        [perPage:protected] => 15
        [incrementing] => 1
        [timestamps] => 1
        [attributes:protected] => Array
        (
            [id] => ve
            [name] => Test
            [currency_symbol] => T
            [currency_name] => Test currency
            [tax_rate] => 12
            [url] => 
            [support_email] => support@example.com
            [noreply_email] => roreply@example.com
            [active] => 1
            [root] => 1
        )

        [original:protected] => Array
        (
            [id] => ve
            [name] => Test
            [currency_symbol] => T
            [currency_name] => Test currency
            [tax_rate] => 12
            [url] => 
            [support_email] => support@example.com
            [noreply_email] => roreply@example.com
            [active] => 1
            [root] => 1
        )

        [relations:protected] => Array
            (
            )

        [hidden:protected] => Array
            (
            )

        [visible:protected] => Array
            (
            )

        [appends:protected] => Array
           (
           )

        [fillable:protected] => Array
            (
            )

        [guarded:protected] => Array
            (
                [0] => *
            )

        [dates:protected] => Array
                             (
                             )

        [dateFormat:protected] => 
        [casts:protected] => Array
             (
             )

        [touches:protected] => Array
           (
           )

        [observables:protected] => Array
           (
           )

        [with:protected] => Array
            (
            )

        [morphClass:protected] => 
        [exists] => 1
        [wasRecentlyCreated] => 
    )
Run Code Online (Sandbox Code Playgroud)

如有必要:

  • Laravel版本:5.2
  • PHP:5.6.15

Tho*_*Kim 20

如果您的主键不是自动递增值,那么您需要让模型知道它不是.否则,它会自动尝试将主键转换为整数.

因此,尝试将此添加到模型中,然后检索该值.

public $incrementing = false;
Run Code Online (Sandbox Code Playgroud)

另外需要注意的是,first()在使用该find()方法时不需要调用.在幕后,它已经为你做了,所以你可以缩短它:

Country::find('ve')->id;
Run Code Online (Sandbox Code Playgroud)