【问题标题】:Cant retrieve column value from Laravel's Eloquent when primary key is varchar当主键为 varchar 时,无法从 Laravel 的 Eloquent 中检索列值
【发布时间】:2016-07-30 08:47:43
【问题描述】:

我遇到了一个问题,我的 Laravel 的 Eloquent 模型没有为我提供名为“id”的列的值,它只是变成整数 (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');
    });

模型真的很简单:

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

【问题讨论】:

    标签: php mysql laravel orm eloquent


    【解决方案1】:

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

    因此,请尝试将其添加到您的模型中,然后检索该值。

    public $incrementing = false;
    

    另外需要注意的是,在使用find() 方法时不需要调用first()。在幕后,它已经为您做到了,因此您可以将其缩短为:

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

    【讨论】:

    • 非常感谢。对... find() 按主键。我是 Laravel 的菜鸟……但到了那里! :c
    • 感谢您的解决方案!尽管如此,这是您只需要了解框架才能使其正常工作的事情之一,而不是直观的。如果列不是数字,为什么要将其转换为整数!?
    • 只是 Laravel 的东西¯\_(ツ)_/¯
    猜你喜欢
    • 1970-01-01
    • 2017-06-19
    • 1970-01-01
    • 2015-10-18
    • 2014-04-11
    • 2019-05-21
    • 1970-01-01
    • 1970-01-01
    • 2019-10-14
    相关资源
    最近更新 更多