onl*_*mas 5 php uuid type-conversion eloquent laravel-5
跟进我昨晚的问题之后,我睡了一个好觉,并“发现”这是由于自动转换/类型转换或其他原因导致的。
总结:
uuid作主键trigger从MySQL插入获得它们$model_object->fresh(); fresh()模型
<?php namespace App\Models;
use Illuminate\Database\Eloquent\Model;
class Address extends Model {
protected $table = 'Addresses';
protected $fillable = ['uuid', 'zipCode', 'houseNumber'];
protected $primaryKey = 'uuid';
//public $incrementing = false;
}
Run Code Online (Sandbox Code Playgroud)
控制器(拧紧的地方)
public function store(Request $request) {
$input = $request->all();
$address = Address::create($input);
var_dump($address); exit;
Run Code Online (Sandbox Code Playgroud)
$address = $address->fresh();
var_dump($address); exit;
Run Code Online (Sandbox Code Playgroud)
var_dump($address->uuid); exit;//result (wow): int(0)
}
Run Code Online (Sandbox Code Playgroud)
Der*_*ima 11
对于以后来这里的人。您可以$keyType使用字符串类型覆盖模型中的变量:
protected $keyType = 'string';
Run Code Online (Sandbox Code Playgroud)
为了使它起作用,我必须手动覆盖模型中主键的转换类型。
protected $casts = [
'id' => 'string'
];
Run Code Online (Sandbox Code Playgroud)
我查看了\Illuminate\Database\Eloquent\Model类,发现最有效的方法是$incrementing = false在您的模型中设置。
在我的测试中,创建和检索模型都按预期工作。
这是 PHP 文档注释的片段。
/**
* Indicates if the IDs are auto-incrementing.
*
* @var bool
*/
public $incrementing = false;
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1395 次 |
| 最近记录: |