Php 无法从Laravel';检索列值;当主键为varchar时,它会很有说服力

Php 无法从Laravel';检索列值;当主键为varchar时,它会很有说服力,php,mysql,laravel,orm,eloquent,Php,Mysql,Laravel,Orm,Eloquent,我遇到了一个问题,我的Laravel雄辩的模型没有给我名为“id”的列的值,它只是变成一个整数(0)而不是字符串。我认为该列在某种程度上受到了保护,但在其他模型中,“id”是一个加强器,它返回的值很好 问题:我不能对主键使用VARCHAR吗 注意:我必须创建更多具有各自模型的表,其中主键需要是字符串;我是新来的拉威尔 迁移: Schema::create('country', function (Blueprint $table) { $table->engine = 'I

我遇到了一个问题,我的Laravel雄辩的模型没有给我名为“id”的列的值,它只是变成一个整数(0)而不是字符串。我认为该列在某种程度上受到了保护,但在其他模型中,“id”是一个加强器,它返回的值很好

问题:我不能对主键使用VARCHAR吗

注意:我必须创建更多具有各自模型的表,其中主键需要是字符串;我是新来的拉威尔

迁移:

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');
    });
模型非常简单:

use Illuminate\Database\Eloquent\Model;

class Country extends Model
{
    protected $table = 'country';
}
但是当我试图找回它的时候

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
但是如果我使用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] => 
    )
如有需要:

  • Laravel版本:5.2
  • PHP:5.6.15

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

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

public $incrementing = false;
另一件需要注意的事情是,当您使用
find()
方法时,不需要调用
first()
。在幕后,它已经为您做到了这一点,因此您可以将其缩短为:

Country::find('ve')->id;

非常感谢你。正确的。。。按主键查找()。我是拉威尔的新秀。。。但是到了那里!:谢谢你的解决方案!不过,这是您必须了解框架才能使其正常工作的内容之一,而不是直观的。如果列不是数字,为什么要将其转换为整数!?只是拉维的东西“\_(ツ)_/¯