当主键是 varchar 时,无法从 Laravel 的 Eloquent 中检索列值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/36510599/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Cant retrieve column value from Laravel's Eloquent when primary key is varchar
提问by David Lavieri
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.
我遇到了一个问题,我的 Laravel 的 Eloquent 模型没有给我名为 'id' 的列的值,它只是变成了一个整数 (0) 而不是一个字符串。我虽然该列以某种方式受到保护,但在“id”是整数的其他模型中,它返回的值很好。
Question: Can't I use VARCHAR for primary keys?
问题:我不能对主键使用 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
注意:我必须用各自的模型创建更多的表,其中主键需要是一个字符串;我有点新 Laravel
Migration:
移民:
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');
});
Model is really simple:
模型非常简单:
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
But if I use print_r() function this is the output:
但是如果我使用 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] => [email protected]
[noreply_email] => [email protected]
[active] => 1
[root] => 1
)
[original:protected] => Array
(
[id] => ve
[name] => Test
[currency_symbol] => T
[currency_name] => Test currency
[tax_rate] => 12
[url] =>
[support_email] => [email protected]
[noreply_email] => [email protected]
[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] =>
)
if necessary:
如有必要:
- Laravel version: 5.2
- PHP: 5.6.15
- Laravel 版本:5.2
- PHP:5.6.15
回答by Thomas Kim
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;
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:
还有一点需要注意的是,first()
使用该find()
方法时不需要调用。在幕后,它已经为您做到了,因此您可以将其缩短为:
Country::find('ve')->id;