doujiang2643 2015-03-13 09:57
浏览 124
已采纳

运行laravel可以说明laravel之外的数据库事务

require dirname(dirname(__DIR__)) . '/vendor/autoload.php';
require dirname(__DIR__) . '/config.php';

use Illuminate\Database\Capsule\Manager as DB;

$dbc = new DB;

$dbc->addConnection(array(
    'driver'    => 'mysql',
    'host'      => 'localhost',
    'database'  => DB_NAME,
    'username'  => DB_USER,
    'password'  => DB_PASSWORD,
    'charset'   => 'utf8',
    'collation' => 'utf8_unicode_ci',
    'prefix'    => ''
));

$dbc->setAsGlobal();
$dbc->bootEloquent();

$dbc->beginTransaction();

Gives me this error,

PHP Fatal error:  Call to undefined method Illuminate\\Database\\Capsule\\Manager::beginTransaction()

Can anyone provide me with some help as to how to use capsule and database transactions outside of laravel. I can't find anything that would suggest this doesnt work?

  • 写回答

1条回答 默认 最新

  • drmticpet66231422 2015-03-13 11:45
    关注

    You need to get a connection instance to work with any database related methods (including beginTransaction). The class that implements them is Illuminate\Database\Connection. This means you need to get the connection instance from the manager and work with that:

    use Illuminate\Database\Capsule\Manager as DB;
    
    $capsule = new DB;
    
    $capsule->addConnection(array(
        'driver'    => 'mysql',
        'host'      => 'localhost',
        'database'  => DB_NAME,
        'username'  => DB_USER,
        'password'  => DB_PASSWORD,
        'charset'   => 'utf8',
        'collation' => 'utf8_unicode_ci',
        'prefix'    => ''
    ));
    
    $capsule->setAsGlobal();
    $capsule->bootEloquent();
    
    $connection = $capsule->getConnection();
    $connection->beginTransaction();
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器