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

标签 php mysql laravel orm eloquent

我遇到了一个问题,我的 Laravel 的 Eloquent 模型没有给我名为“id”的列的值,它只是变成一个整数 (0) 而不是一个字符串。我虽然该列以某种方式受到保护,但在“id”是整数的其他模型中,它返回值就好了。

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

Note: I have to create more tables with respective models where the primary key need to be a string; I'm kinda new 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');
    });

模型真的很简单:

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;

关于php - 当主键为 varchar 时,无法从 Laravel 的 Eloquent 中检索列值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36510599/

相关文章:

mySQL 两个不同表中两个值的总和

javascript - 使用一个 div 获取多个值(动态)时,jssor slider 会出现问题

javascript - 网络音频 API : store arraybuffer on server as file and retrieve it later?

mysql - 如何向现有表添加唯一键(具有非唯一行)

javascript - 如何动态更改 Laravel 表单

php - 我必须在 Laravel 中使用什么命名空间或外观才能访问命名空间类中的模型?

php - Laravel 5 上未显示自定义错误页面

php - 在 PHP 中创建一个代理来欺骗 iPhone 用户代理?

php - 日期减去 1 年?

php - 根据其他字段是否有值来更新不同的字段