2017-08-03 10 views
0

我的情况是,我没有使用自动增量在我的模型 ID。检索Laravel触发生成的UID的保存方法

相反,我使用MySQL的触发器自动enerated一个串UID,G

所以,当我创建一个模型,例如

$foo = new Foo(); 
$foo->bar = "bar"; 
$foo->save(); 
echo $foo->id; // null 

我的id为null。发生这种情况是因为我的public $incrementing必须设置为false。

如何检索此触发器已保存值为save()

+0

可以像['Model-> fresh()'](https://laravel.com/api/5.4/Illuminate/Database/Eloquent/Model.html#method_fresh)这样做吗? – Bytewave

+0

@Bytewave它返回'null',可能是因为它没有找到没有ID的记录。 – Luiz

+0

@Luiz,你试图创建'()'方法,'富::创建([ '酒吧'=> '巴']);'? –

回答

0

我读API of Laravel 5.4(假设你使用这个版本),而我发现:

array getDirty()方法:

$foo->save(); 
$newAttributes = $foo->getDirty(); 
dd($newAttributes); // dump an array. 

void refresh()方法:

$foo->save(); 
$foo->refresh(); 
dd($foo->id) 

最后解决方案,where()从QueryBuilder的找到您的实例:

$foo->save(); 
Foo::where('bar', $foo->bar) 
    ->where('toto', $foo->toto) 
    /* ... */ 
    ->get(); 
dd($foo->id);