当主键为varchar时,无法从Laravel的Eloquent中检索列值 [英] Cant retrieve column value from Laravel's Eloquent when primary key is varchar

查看:39
本文介绍了当主键为varchar时,无法从Laravel的Eloquent中检索列值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到了一个问题,我的Laravel的口才模型没有给我提供名为"id"的列的值,它只是变成了整数(0)而不是字符串.我虽然该列受到了某种保护,但是在其他模型中,"id"是一个整数,它返回的值就很好.

I encountered an issue where my Laravel's Eloquent Model does not give me the value of the column called 'id' it just turn to be an integer (0) instead of a string. I though the column was somehow protected but in other Models where the 'id' is an intenger it return the value just fine.

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

Question: Can't I use VARCHAR for primary keys?

注意:我必须使用各自的模型创建更多的表,其中主键需要为字符串;我是新来的Laravel

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';
}

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

But when i try to retrieve it...

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()函数,则输出为:

But if I use print_r() function this is the output:

    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

推荐答案

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

If your primary key is not an auto-incrementing value, then you need to let the model know that it's not. Otherwise, it automatically tries to convert the primary key into an integer.

因此,请尝试将其添加到模型中,然后获取值.

So, try adding this to your model, and then retrieving the value.

public $incrementing = false;

要注意的另一件事是,当您使用 find()方法时,不需要调用 first().在幕后,它已经为您做到了,因此您可以将其简化为:

Another thing to note is that you don't need to call first() when you use the find() method. Behind the scenes, it already does that for you so you can shorten it to this:

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

这篇关于当主键为varchar时,无法从Laravel的Eloquent中检索列值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆