企业🤖AI智能体构建引擎,智能编排和调试,一键部署,支持私有化部署方案 广告
<article><h1>数据库:入门</h1><ul><li><a href="#introduction">简介</a><ul><li><a href="#configuration">配置信息</a></li><li><a href="#read-and-write-connections">数据库读写分离</a></li><li><a href="#using-multiple-database-connections">使用多数据库连接</a></li></ul></li><li><a href="#running-queries">运行原生 SQL 语句</a><ul><li><a href="#listening-for-query-events">监听查询事件</a></li></ul></li><li><a href="#database-transactions">数据库事务</a></li></ul><p><a name="introduction"></a></p><h2><a href="#introduction">简介</a></h2><p>Laravel 对主流数据库系统连接和查询都提供了很好的支持,尤其是流畅的 <a href="/docs/5.4/queries">查询语句构造器</a> , Laravel 支持四种类型的数据库:</p><ul><li>MySQL</li><li>Postgres</li><li>SQLite</li><li>SQL Server</li></ul><p><a name="configuration"></a></p><h3>配置信息</h3><p>Laravel 应用程序的数据库配置文件放置在 <code class=" language-php">config<span class="token operator">/</span>database<span class="token punctuation">.</span>php</code> 文件中。在这个配置文件内你可以定义所有的数据库连接,以及指定默认使用哪个连接。在此文件内提供了所有支持的数据库系统示例。</p><p>默认情况下,Laravel 的 <a href="/docs/5.4/installation#environment-configuration">环境配置</a> 示例会使用 <a href="/docs/5.4/homestead">Laravel Homestead</a>,对于 Laravel 开发来说这是一个相当便利的本地虚拟机。当然你也可以根据需求来随时修改本机端的数据库设置。</p><h4>SQLite 配置</h4><p>请使用 <code class=" language-php">touch database<span class="token operator">/</span>database<span class="token punctuation">.</span>sqlite</code> 命令创建一个 SQLite 文件, 您可以通过使用数据库的绝对路径来轻松地配置您的环境变量来指向这个新创建的数据库:</p><pre class=" language-php"><code class=" language-php"><span class="token constant">DB_CONNECTION</span><span class="token operator">=</span>sqlite <span class="token constant">DB_DATABASE</span><span class="token operator">=</span><span class="token operator">/</span>absolute<span class="token operator">/</span>path<span class="token operator">/</span>to<span class="token operator">/</span>database<span class="token punctuation">.</span>sqlite</code></pre><h4>SQL Server 配置</h4><p>Laravel 支持 SQL Server 数据库,你需要在 <code class=" language-php">config<span class="token operator">/</span>database<span class="token punctuation">.</span>php</code> 中为连接 SQL Server 数据库做配置:</p><pre class=" language-php"><code class=" language-php"><span class="token string">'sqlsrv'</span> <span class="token operator">=</span><span class="token operator">&gt;</span> <span class="token punctuation">[</span> <span class="token string">'driver'</span> <span class="token operator">=</span><span class="token operator">&gt;</span> <span class="token string">'sqlsrv'</span><span class="token punctuation">,</span> <span class="token string">'host'</span> <span class="token operator">=</span><span class="token operator">&gt;</span> <span class="token function">env<span class="token punctuation">(</span></span><span class="token string">'DB_HOST'</span><span class="token punctuation">,</span> <span class="token string">'localhost'</span><span class="token punctuation">)</span><span class="token punctuation">,</span> <span class="token string">'database'</span> <span class="token operator">=</span><span class="token operator">&gt;</span> <span class="token function">env<span class="token punctuation">(</span></span><span class="token string">'DB_DATABASE'</span><span class="token punctuation">,</span> <span class="token string">'forge'</span><span class="token punctuation">)</span><span class="token punctuation">,</span> <span class="token string">'username'</span> <span class="token operator">=</span><span class="token operator">&gt;</span> <span class="token function">env<span class="token punctuation">(</span></span><span class="token string">'DB_USERNAME'</span><span class="token punctuation">,</span> <span class="token string">'forge'</span><span class="token punctuation">)</span><span class="token punctuation">,</span> <span class="token string">'password'</span> <span class="token operator">=</span><span class="token operator">&gt;</span> <span class="token function">env<span class="token punctuation">(</span></span><span class="token string">'DB_PASSWORD'</span><span class="token punctuation">,</span> <span class="token string">''</span><span class="token punctuation">)</span><span class="token punctuation">,</span> <span class="token string">'charset'</span> <span class="token operator">=</span><span class="token operator">&gt;</span> <span class="token string">'utf8'</span><span class="token punctuation">,</span> <span class="token string">'prefix'</span> <span class="token operator">=</span><span class="token operator">&gt;</span> <span class="token string">''</span><span class="token punctuation">,</span> <span class="token punctuation">]</span><span class="token punctuation">,</span></code></pre><p><a name="read-and-write-connections"></a></p><h3>数据库读写分离</h3><p>有时候你希望把一个数据库作为只读数据库,而另一个数据库则负责写入、更新以及删除。Laravel 会让你轻而易举的实现,并适用于原始查找、查询语句构造器或是 Eloquent ORM。</p><p>通过下面这个例子,我们来学习如何配置数据库读写连接的分离:</p><pre class=" language-php"><code class=" language-php"><span class="token string">'mysql'</span> <span class="token operator">=</span><span class="token operator">&gt;</span> <span class="token punctuation">[</span> <span class="token string">'read'</span> <span class="token operator">=</span><span class="token operator">&gt;</span> <span class="token punctuation">[</span> <span class="token string">'host'</span> <span class="token operator">=</span><span class="token operator">&gt;</span> <span class="token string">'192.168.1.1'</span><span class="token punctuation">,</span> <span class="token punctuation">]</span><span class="token punctuation">,</span> <span class="token string">'write'</span> <span class="token operator">=</span><span class="token operator">&gt;</span> <span class="token punctuation">[</span> <span class="token string">'host'</span> <span class="token operator">=</span><span class="token operator">&gt;</span> <span class="token string">'196.168.1.2'</span> <span class="token punctuation">]</span><span class="token punctuation">,</span> <span class="token string">'driver'</span> <span class="token operator">=</span><span class="token operator">&gt;</span> <span class="token string">'mysql'</span><span class="token punctuation">,</span> <span class="token string">'database'</span> <span class="token operator">=</span><span class="token operator">&gt;</span> <span class="token string">'database'</span><span class="token punctuation">,</span> <span class="token string">'username'</span> <span class="token operator">=</span><span class="token operator">&gt;</span> <span class="token string">'root'</span><span class="token punctuation">,</span> <span class="token string">'password'</span> <span class="token operator">=</span><span class="token operator">&gt;</span> <span class="token string">''</span><span class="token punctuation">,</span> <span class="token string">'charset'</span> <span class="token operator">=</span><span class="token operator">&gt;</span> <span class="token string">'utf8'</span><span class="token punctuation">,</span> <span class="token string">'collation'</span> <span class="token operator">=</span><span class="token operator">&gt;</span> <span class="token string">'utf8_unicode_ci'</span><span class="token punctuation">,</span> <span class="token string">'prefix'</span> <span class="token operator">=</span><span class="token operator">&gt;</span> <span class="token string">''</span><span class="token punctuation">,</span> <span class="token punctuation">]</span><span class="token punctuation">,</span></code></pre><p>注意,有两个键加入了这个配置文件数组内: <code class=" language-php">read</code> 和 <code class=" language-php">write</code> 。 它们两个都是一个数组且只包含了一个键: <code class=" language-php">host</code> 。 而 <code class=" language-php">read</code> 和 <code class=" language-php">write</code> 连接的其他配置都包含在 <code class=" language-php">mysql</code> 数组中。</p><p>所以,如果需要在主要的数组内重写值,只需在 <code class=" language-php">read</code> and <code class=" language-php">write</code> 数组内放置设置参数即可。在这个例子中, <code class=" language-php"><span class="token number">192.168</span><span class="token punctuation">.</span><span class="token number">1.1</span></code> 将会只提供数据读取数据的功能,而 <code class=" language-php"><span class="token number">192.168</span><span class="token punctuation">.</span><span class="token number">1.2</span></code> 提供数据库写入。数据库的凭证、前缀、编码设置,以及所有其它的选项都被存放在 <code class=" language-php">mysql</code> 数组内,这两个连接将会共用这些选项。</p><p><a name="using-multiple-database-connections"></a></p><h3>使用多数据库连接</h3><p>当使用多个数据连接时,你可以使用 <code class=" language-php"><span class="token constant">DB</span></code> facade 的 <code class=" language-php">connection</code> 方法。在 <code class=" language-php">config<span class="token operator">/</span>database<span class="token punctuation">.</span>php</code> 中定义好的数据库连接 <code class=" language-php">name</code> 作为 <code class=" language-php">connection</code> 的参数进行传递。</p><pre class=" language-php"><code class=" language-php"><span class="token variable">$users</span> <span class="token operator">=</span> <span class="token scope">DB<span class="token punctuation">::</span></span><span class="token function">connection<span class="token punctuation">(</span></span><span class="token string">'foo'</span><span class="token punctuation">)</span><span class="token operator">-</span><span class="token operator">&gt;</span><span class="token function">select<span class="token punctuation">(</span></span><span class="token punctuation">.</span><span class="token punctuation">.</span><span class="token punctuation">.</span><span class="token punctuation">)</span><span class="token punctuation">;</span></code></pre><p>你也可以在连接的实例中使用 <code class=" language-php">getPdo</code> 方法访问原始的底层 PDO 实例:</p><pre class=" language-php"><code class=" language-php"><span class="token variable">$pdo</span> <span class="token operator">=</span> <span class="token scope">DB<span class="token punctuation">::</span></span><span class="token function">connection<span class="token punctuation">(</span></span><span class="token punctuation">)</span><span class="token operator">-</span><span class="token operator">&gt;</span><span class="token function">getPdo<span class="token punctuation">(</span></span><span class="token punctuation">)</span><span class="token punctuation">;</span></code></pre><p><a name="running-queries"></a></p><h2><a href="#running-queries">运行原生 SQL 语句</a></h2><p>配置好数据库连接以后,你可以使用 <code class=" language-php"><span class="token constant">DB</span></code> facade 来执行查询。 <code class=" language-php"><span class="token constant">DB</span></code> facade 提供了 <code class=" language-php">select</code> 、 <code class=" language-php">update</code> 、 <code class=" language-php">insert</code> 、 <code class=" language-php">delete</code> 和 <code class=" language-php">statement</code> 的查询方法。</p><h4>运行 Select</h4><p>运行一个基础的查询语句,你可以使用 <code class=" language-php"><span class="token constant">DB</span></code> facade 的 <code class=" language-php">select</code> 方法:</p><pre class=" language-php"><code class=" language-php"><span class="token delimiter">&lt;?php</span> <span class="token keyword">namespace</span> <span class="token package">App<span class="token punctuation">\</span>Http<span class="token punctuation">\</span>Controllers</span><span class="token punctuation">;</span> <span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Facades<span class="token punctuation">\</span>DB</span><span class="token punctuation">;</span> <span class="token keyword">use</span> <span class="token package">App<span class="token punctuation">\</span>Http<span class="token punctuation">\</span>Controllers<span class="token punctuation">\</span>Controller</span><span class="token punctuation">;</span> <span class="token keyword">class</span> <span class="token class-name">UserController</span> <span class="token keyword">extends</span> <span class="token class-name">Controller</span> <span class="token punctuation">{</span> <span class="token comment" spellcheck="true">/** * 在应用用户中查询出已激活的用户列表。 * * @return Response */</span> <span class="token keyword">public</span> <span class="token keyword">function</span> <span class="token function">index<span class="token punctuation">(</span></span><span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token variable">$users</span> <span class="token operator">=</span> <span class="token scope">DB<span class="token punctuation">::</span></span><span class="token function">select<span class="token punctuation">(</span></span><span class="token string">'select * from users where active = ?'</span><span class="token punctuation">,</span> <span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token keyword">return</span> <span class="token function">view<span class="token punctuation">(</span></span><span class="token string">'user.index'</span><span class="token punctuation">,</span> <span class="token punctuation">[</span><span class="token string">'users'</span> <span class="token operator">=</span><span class="token operator">&gt;</span> <span class="token variable">$users</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token punctuation">}</span></code></pre><p>传递到 <code class=" language-php">select</code> 方法的第一个参数是一个原生的 SQL 查询,而第二个参数则是传递的所有绑定到查询中的参数值。通常,这些都是 <code class=" language-php">where</code> 字句约束中的值。参数绑定可以避免 SQL 注入攻击。</p><p><code class=" language-php">select</code> 方法以数组的形式返回结果集,数组中的每一个结果都是一个PHP <code class=" language-php">StdClass</code> 对象,你可以像下面这样访问结果值:</p><pre class=" language-php"><code class=" language-php"><span class="token keyword">foreach</span> <span class="token punctuation">(</span><span class="token variable">$users</span> <span class="token keyword">as</span> <span class="token variable">$user</span><span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token keyword">echo</span> <span class="token variable">$user</span><span class="token operator">-</span><span class="token operator">&gt;</span><span class="token property">name</span><span class="token punctuation">;</span> <span class="token punctuation">}</span></code></pre><h4>使用命名绑定</h4><p>除了使用 <code class=" language-php"><span class="token operator">?</span></code> 来表示参数绑定外,你也可以使用命名绑定运行查找:</p><pre class=" language-php"><code class=" language-php"><span class="token variable">$results</span> <span class="token operator">=</span> <span class="token scope">DB<span class="token punctuation">::</span></span><span class="token function">select<span class="token punctuation">(</span></span><span class="token string">'select * from users where id = :id'</span><span class="token punctuation">,</span> <span class="token punctuation">[</span><span class="token string">'id'</span> <span class="token operator">=</span><span class="token operator">&gt;</span> <span class="token number">1</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span></code></pre><h4>运行 insert</h4><p>运行 <code class=" language-php">Insert</code> 语句,你可以是使用 <code class=" language-php"><span class="token constant">DB</span></code> facade 的 <code class=" language-php">insert</code> 方法 。 像 <code class=" language-php">select</code> 一样, 该方法将原生SQL语句作为第一个参数,将参数绑定作为第二个参数:</p><pre class=" language-php"><code class=" language-php"><span class="token scope">DB<span class="token punctuation">::</span></span><span class="token function">insert<span class="token punctuation">(</span></span><span class="token string">'insert into users (id, name) values (?, ?)'</span><span class="token punctuation">,</span> <span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">,</span> <span class="token string">'Dayle'</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span></code></pre><h4>运行 Update</h4><p><code class=" language-php">update</code> 方法用于更新已经存在于数据库的记录。该方法会返回此语句执行所影响的行数:</p><pre class=" language-php"><code class=" language-php"><span class="token variable">$affected</span> <span class="token operator">=</span> <span class="token scope">DB<span class="token punctuation">::</span></span><span class="token function">update<span class="token punctuation">(</span></span><span class="token string">'update users set votes = 100 where name = ?'</span><span class="token punctuation">,</span> <span class="token punctuation">[</span><span class="token string">'John'</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span></code></pre><h4>运行 Delete</h4><p><code class=" language-php">delete</code> 方法用于删除已经存在于数据库的记录。如同 <code class=" language-php">update</code> 一样,删除的行数将会被返回。</p><pre class=" language-php"><code class=" language-php"><span class="token variable">$deleted</span> <span class="token operator">=</span> <span class="token scope">DB<span class="token punctuation">::</span></span><span class="token function">delete<span class="token punctuation">(</span></span><span class="token string">'delete from users'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></code></pre><h4>运行一般声明</h4><p>有些数据库没有返回值, 对于这种类型的操作,可以使用 <code class=" language-php"><span class="token constant">DB</span></code> facade 的 <code class=" language-php">statement</code> 方法。</p><pre class=" language-php"><code class=" language-php"><span class="token scope">DB<span class="token punctuation">::</span></span><span class="token function">statement<span class="token punctuation">(</span></span><span class="token string">'drop table users'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></code></pre><p><a name="listening-for-query-events"></a></p><h3>监听查询事件</h3><p>如果你希望能够监控到程序执行的每一条 SQL 语句,那么你可以使用 <code class=" language-php">listen</code> 方法。 该方法对查询日志和调试非常有用,你可以在 <a href="/docs/5.4/providers">服务容器</a> 中注册该方法:</p><pre class=" language-php"><code class=" language-php"><span class="token delimiter">&lt;?php</span> <span class="token keyword">namespace</span> <span class="token package">App<span class="token punctuation">\</span>Providers</span><span class="token punctuation">;</span> <span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Facades<span class="token punctuation">\</span>DB</span><span class="token punctuation">;</span> <span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>ServiceProvider</span><span class="token punctuation">;</span> <span class="token keyword">class</span> <span class="token class-name">AppServiceProvider</span> <span class="token keyword">extends</span> <span class="token class-name">ServiceProvider</span> <span class="token punctuation">{</span> <span class="token comment" spellcheck="true">/** * 启动应用服务。 * * @return void */</span> <span class="token keyword">public</span> <span class="token keyword">function</span> <span class="token function">boot<span class="token punctuation">(</span></span><span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token scope">DB<span class="token punctuation">::</span></span><span class="token function">listen<span class="token punctuation">(</span></span><span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token variable">$query</span><span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token comment" spellcheck="true"> // $query-&gt;sql </span> <span class="token comment" spellcheck="true"> // $query-&gt;bindings </span> <span class="token comment" spellcheck="true"> // $query-&gt;time </span> <span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token comment" spellcheck="true">/** * 注册服务提供者。 * * @return void */</span> <span class="token keyword">public</span> <span class="token keyword">function</span> <span class="token function">register<span class="token punctuation">(</span></span><span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token comment" spellcheck="true"> // </span> <span class="token punctuation">}</span> <span class="token punctuation">}</span></code></pre><p><a name="database-transactions"></a></p><h2><a href="#database-transactions">数据库事务</a></h2><p>想要在一个数据库事务中运行一连串操作,可以使用 <code class=" language-php"><span class="token constant">DB</span></code> facade 的 <code class=" language-php">transaction</code> 方法。如果在事务的 <code class=" language-php">Closure</code> 中抛出了异常,那么事务会自动的执行回滚操作。如果 <code class=" language-php">Closure</code> 成功的执行,那么事务就会自动的进行提交操作。你不需要在使用 <code class=" language-php">transaction</code> 方法时考虑手动执行回滚或者提交操作:</p><pre class=" language-php"><code class=" language-php"><span class="token scope">DB<span class="token punctuation">::</span></span><span class="token function">transaction<span class="token punctuation">(</span></span><span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token scope">DB<span class="token punctuation">::</span></span><span class="token function">table<span class="token punctuation">(</span></span><span class="token string">'users'</span><span class="token punctuation">)</span><span class="token operator">-</span><span class="token operator">&gt;</span><span class="token function">update<span class="token punctuation">(</span></span><span class="token punctuation">[</span><span class="token string">'votes'</span> <span class="token operator">=</span><span class="token operator">&gt;</span> <span class="token number">1</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token scope">DB<span class="token punctuation">::</span></span><span class="token function">table<span class="token punctuation">(</span></span><span class="token string">'posts'</span><span class="token punctuation">)</span><span class="token operator">-</span><span class="token operator">&gt;</span><span class="token function">delete<span class="token punctuation">(</span></span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span></code></pre><h4>手动操作事务</h4><p>如果你想要手动开始一个事务的回滚和提交操作,你可以使用 <code class=" language-php"><span class="token constant">DB</span></code> facade 的 <code class=" language-php">beginTransaction</code> 方法。</p><pre class=" language-php"><code class=" language-php"><span class="token scope">DB<span class="token punctuation">::</span></span><span class="token function">beginTransaction<span class="token punctuation">(</span></span><span class="token punctuation">)</span><span class="token punctuation">;</span></code></pre><p>你也可以通过 <code class=" language-php">rollBack</code> 方法来回滚事务:</p><pre class=" language-php"><code class=" language-php"><span class="token scope">DB<span class="token punctuation">::</span></span><span class="token function">rollBack<span class="token punctuation">(</span></span><span class="token punctuation">)</span><span class="token punctuation">;</span></code></pre><p>最后,可以通过 <code class=" language-php">commit</code> 方法来提交这个事务:</p><pre class=" language-php"><code class=" language-php"><span class="token scope">DB<span class="token punctuation">::</span></span><span class="token function">commit<span class="token punctuation">(</span></span><span class="token punctuation">)</span><span class="token punctuation">;</span></code></pre><blockquote class="has-icon tip"><p><div class="flag"><span class="svg"><svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" xmlns:a="http://ns.adobe.com/AdobeSVGViewerExtensions/3.0/" version="1.1" x="0px" y="0px" width="56.6px" height="87.5px" viewBox="0 0 56.6 87.5" enable-background="new 0 0 56.6 87.5" xml:space="preserve"><path fill="#FFFFFF" d="M28.7 64.5c-1.4 0-2.5-1.1-2.5-2.5v-5.7 -5V41c0-1.4 1.1-2.5 2.5-2.5s2.5 1.1 2.5 2.5v10.1 5 5.8C31.2 63.4 30.1 64.5 28.7 64.5zM26.4 0.1C11.9 1 0.3 13.1 0 27.7c-0.1 7.9 3 15.2 8.2 20.4 0.5 0.5 0.8 1 1 1.7l3.1 13.1c0.3 1.1 1.3 1.9 2.4 1.9 0.3 0 0.7-0.1 1.1-0.2 1.1-0.5 1.6-1.8 1.4-3l-2-8.4 -0.4-1.8c-0.7-2.9-2-5.7-4-8 -1-1.2-2-2.5-2.7-3.9C5.8 35.3 4.7 30.3 5.4 25 6.7 14.5 15.2 6.3 25.6 5.1c13.9-1.5 25.8 9.4 25.8 23 0 4.1-1.1 7.9-2.9 11.2 -0.8 1.4-1.7 2.7-2.7 3.9 -2 2.3-3.3 5-4 8L41.4 53l-2 8.4c-0.3 1.2 0.3 2.5 1.4 3 0.3 0.2 0.7 0.2 1.1 0.2 1.1 0 2.2-0.8 2.4-1.9l3.1-13.1c0.2-0.6 0.5-1.2 1-1.7 5-5.1 8.2-12.1 8.2-19.8C56.4 12 42.8-1 26.4 0.1zM43.7 69.6c0 0.5-0.1 0.9-0.3 1.3 -0.4 0.8-0.7 1.6-0.9 2.5 -0.7 3-2 8.6-2 8.6 -1.3 3.2-4.4 5.5-7.9 5.5h-4.1H28h-0.5 -3.6c-3.5 0-6.7-2.4-7.9-5.7l-0.1-0.4 -1.8-7.8c-0.4-1.1-0.8-2.1-1.2-3.1 -0.1-0.3-0.2-0.5-0.2-0.9 0.1-1.3 1.3-2.1 2.6-2.1H41C42.4 67.5 43.6 68.2 43.7 69.6zM37.7 72.5H26.9c-4.2 0-7.2 3.9-6.3 7.9 0.6 1.3 1.8 2.1 3.2 2.1h4.1 0.5 0.5 3.6c1.4 0 2.7-0.8 3.2-2.1L37.7 72.5z"></path></svg></span></div> 使用 <code class=" language-php"><span class="token constant">DB</span></code> facade 的事务方法也适用于 <a href="/docs/5.4/queries">查询语句构造器</a> 和 <a href="/docs/5.4/eloquent">Eloquent ORM</a> 。</p></blockquote><h2>译者署名</h2><table><thead><tr><th>用户名</th><th>头像</th><th>职能</th><th>签名</th></tr></thead><tbody><tr><td><a href="https://laravel-china.org/users/79">@skyverd</a></td><td><img class="avatar-66 rm-style" src="https://dn-phphub.qbox.me/uploads/avatars/79_1427370664.jpeg?imageView2/1/w/100/h/100"></td><td>翻译</td><td>全桟工程师,<a href="https://skyverd.com">时光博客</a></td></tr></tbody></table></article>