在 laravel 的 DB::transaction 中,为外部变量赋值


例如,我想在 laravel 的事务中,对某个外部变量赋值,然后在后续的逻辑中判断该变量的属性

$user = null; // init DB::transaction(function() use($user) { // do something with user }); // check user if ($user->name) { // bla, bla } 

这样会报错

Trying to get property of non-object at

也就是说,在 PHP 中,即使是对象也不会默认采用引用的方式传参。

需要修改为

DB::transaction(function() use(&$user) { // do something with user }); // check user 

果然,我还是个 PHP 初学者。。。

参考

https://stackoverflow.com/questions/31059540/laravel-get-variable-from-a-db-transaction-closure


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM