ThinkChat2.0新版上线,更智能更精彩,支持会话、画图、阅读、搜索等,送10W Token,即刻开启你的AI之旅 广告
# 数据库 - 数据库操作 ``` <pre class="calibre14">``` use Illuminate\Support\Facades\DB<span class="token2">;</span> DB<span class="token2">:</span><span class="token2">:</span><span class="token1">select</span><span class="token2">(</span><span class="token4">"select * from users where name=?"</span><span class="token2">,</span><span class="token2">[</span><span class="token4">'admin'</span><span class="token2">]</span><span class="token2">)</span><span class="token2">;</span> <span class="token6">//也支持name=:name,[':name'=>'admin']绑定,返回obj</span> DB<span class="token2">:</span><span class="token2">:</span><span class="token1">insert</span><span class="token2">(</span><span class="token4">'insert into users (id, name) values (?, ?)'</span><span class="token2">,</span> <span class="token2">[</span><span class="token3">1</span><span class="token2">,</span> <span class="token4">'Dayle'</span><span class="token2">]</span><span class="token2">)</span><span class="token2">;</span> $affected <span class="token">=</span> DB<span class="token2">:</span><span class="token2">:</span><span class="token1">update</span><span class="token2">(</span><span class="token4">'update users set votes = 100 where name = ?'</span><span class="token2">,</span> <span class="token2">[</span><span class="token4">'John'</span><span class="token2">]</span><span class="token2">)</span><span class="token2">;</span> 返回影响行数 $deleted <span class="token">=</span> DB<span class="token2">:</span><span class="token2">:</span><span class="token1">delete</span><span class="token2">(</span><span class="token4">'delete from users'</span><span class="token2">)</span><span class="token2">;</span>返回影响行数 DB<span class="token2">:</span><span class="token2">:</span><span class="token1">statement</span><span class="token2">(</span><span class="token4">'drop table users'</span><span class="token2">)</span><span class="token2">;</span> 其他无返回语句 DB<span class="token2">:</span><span class="token2">:</span><span class="token1">transaction</span><span class="token2">(</span><span class="token5">function</span> <span class="token2">(</span><span class="token2">)</span> <span class="token2">{</span> 事务 DB<span class="token2">:</span><span class="token2">:</span><span class="token1">table</span><span class="token2">(</span><span class="token4">'users'</span><span class="token2">)</span><span class="token">-</span><span class="token">></span><span class="token1">update</span><span class="token2">(</span><span class="token2">[</span><span class="token4">'votes'</span> <span class="token">=</span><span class="token">></span> <span class="token3">1</span><span class="token2">]</span><span class="token2">)</span><span class="token2">;</span> DB<span class="token2">:</span><span class="token2">:</span><span class="token1">table</span><span class="token2">(</span><span class="token4">'posts'</span><span class="token2">)</span><span class="token">-</span><span class="token">></span><span class="token1">delete</span><span class="token2">(</span><span class="token2">)</span><span class="token2">;</span> <span class="token2">}</span><span class="token2">)</span><span class="token2">;</span> $list <span class="token">=</span> DB<span class="token2">:</span><span class="token2">:</span><span class="token1">table</span><span class="token2">(</span><span class="token4">"users"</span><span class="token2">)</span><span class="token">-</span><span class="token">></span><span class="token1">get</span><span class="token2">(</span><span class="token2">)</span><span class="token">-</span><span class="token">></span><span class="token1">toArray</span><span class="token2">(</span><span class="token2">)</span><span class="token2">;</span><span class="token6">//获取全部转换成数组</span> DB<span class="token2">:</span><span class="token2">:</span><span class="token1">table</span><span class="token2">(</span><span class="token4">"users"</span><span class="token2">)</span><span class="token">-</span><span class="token">></span><span class="token1">first</span><span class="token2">(</span><span class="token2">)</span> 获取一行 DB<span class="token2">:</span><span class="token2">:</span><span class="token1">table</span><span class="token2">(</span><span class="token4">"users"</span><span class="token2">)</span><span class="token">-</span><span class="token">></span><span class="token1">value</span><span class="token2">(</span><span class="token4">'email'</span><span class="token2">)</span><span class="token2">;</span> 获取一个字段 DB<span class="token2">:</span><span class="token2">:</span><span class="token1">table</span><span class="token2">(</span><span class="token4">"users"</span><span class="token2">)</span><span class="token">-</span><span class="token">></span><span class="token1">pluck</span><span class="token2">(</span><span class="token4">'email'</span><span class="token2">)</span><span class="token">-</span><span class="token">></span><span class="token1">toArray</span><span class="token2">(</span><span class="token2">)</span><span class="token2">;</span> 获取一列<span class="token2">,</span>如果设置两个<span class="token1">pluck</span><span class="token2">(</span><span class="token4">'name'</span><span class="token2">,</span><span class="token4">'id'</span><span class="token2">)</span> id是键name是值的键值数组 <span class="token1">count</span><span class="token2">(</span><span class="token2">)</span> 结果集数量 <span class="token1">max</span><span class="token2">(</span><span class="token4">'price'</span><span class="token2">)</span>最大值 <span class="token1">min</span><span class="token2">(</span><span class="token4">'price'</span><span class="token2">)</span>最小值 <span class="token1">avg</span><span class="token2">(</span><span class="token4">'price'</span><span class="token2">)</span>平均数 <span class="token1">sum</span><span class="token2">(</span><span class="token4">'price'</span><span class="token2">)</span>求和 DB<span class="token2">:</span><span class="token2">:</span><span class="token1">table</span><span class="token2">(</span><span class="token4">'users'</span><span class="token2">)</span><span class="token">-</span><span class="token">></span><span class="token1">select</span><span class="token2">(</span><span class="token4">'name'</span><span class="token2">,</span> <span class="token4">'email as user_email'</span><span class="token2">)</span><span class="token">-</span><span class="token">></span><span class="token1">get</span><span class="token2">(</span><span class="token2">)</span><span class="token2">;</span> 返回特定字段 <span class="token1">whereRaw</span><span class="token2">(</span><span class="token4">'price > IF(state = "TX", ?, 100)'</span><span class="token2">,</span> <span class="token2">[</span><span class="token3">200</span><span class="token2">]</span><span class="token2">)</span> 原生where <span class="token1">orderBy</span><span class="token2">(</span><span class="token4">'name'</span><span class="token2">,</span> <span class="token4">'desc'</span><span class="token2">)</span> <span class="token1">inRandomOrder</span><span class="token2">(</span><span class="token2">)</span>随机排序 <span class="token1">join</span><span class="token2">(</span><span class="token4">'contacts'</span><span class="token2">,</span> <span class="token4">'users.id'</span><span class="token2">,</span> <span class="token4">'='</span><span class="token2">,</span> <span class="token4">'contacts.user_id'</span><span class="token2">)</span> 内连接 <span class="token1">leftJoin</span><span class="token2">(</span><span class="token4">'posts'</span><span class="token2">,</span> <span class="token4">'users.id'</span><span class="token2">,</span> <span class="token4">'='</span><span class="token2">,</span> <span class="token4">'posts.user_id'</span><span class="token2">)</span> 左连接 <span class="token1">groupBy</span><span class="token2">(</span><span class="token4">'account_id'</span><span class="token2">)</span> 分组 <span class="token1">groupBy</span><span class="token2">(</span><span class="token4">'first_name'</span><span class="token2">,</span> <span class="token4">'status'</span><span class="token2">)</span> <span class="token1">having</span><span class="token2">(</span><span class="token4">'account_id'</span><span class="token2">,</span> <span class="token4">'>'</span><span class="token2">,</span> <span class="token3">100</span><span class="token2">)</span> 刷选 <span class="token1">take</span><span class="token2">(</span><span class="token3">5</span><span class="token2">)</span> 返回<span class="token3">5</span>条 <span class="token">-</span><span class="token">></span><span class="token1">offset</span><span class="token2">(</span><span class="token3">10</span><span class="token2">)</span> <span class="token">-</span><span class="token">></span><span class="token1">limit</span><span class="token2">(</span><span class="token3">5</span><span class="token2">)</span> 限制 <span class="token1">insert</span><span class="token2">(</span> <span class="token2">[</span><span class="token4">'email'</span> <span class="token">=</span><span class="token">></span> <span class="token4">'john@example.com'</span><span class="token2">,</span> <span class="token4">'votes'</span> <span class="token">=</span><span class="token">></span> <span class="token3">0</span><span class="token2">]</span><span class="token2">)</span><span class="token2">;</span> 插入 支持多条数据 $id<span class="token">=</span>DB<span class="token2">:</span><span class="token2">:</span><span class="token1">table</span><span class="token2">(</span><span class="token4">"users"</span><span class="token2">)</span><span class="token">-</span><span class="token">></span> <span class="token1">insertGetId</span><span class="token2">(</span> <span class="token2">[</span><span class="token4">'email'</span> <span class="token">=</span><span class="token">></span> <span class="token4">'john@example.com'</span><span class="token2">,</span> <span class="token4">'votes'</span> <span class="token">=</span><span class="token">></span> <span class="token3">0</span><span class="token2">]</span><span class="token2">)</span><span class="token2">;</span> 插入后返回id <span class="token1">update</span><span class="token2">(</span><span class="token2">[</span><span class="token4">'votes'</span> <span class="token">=</span><span class="token">></span> <span class="token3">1</span><span class="token2">]</span><span class="token2">)</span><span class="token2">;</span> 更新 <span class="token1">increment</span><span class="token2">(</span><span class="token4">'votes'</span><span class="token2">,</span> <span class="token3">5</span><span class="token2">)</span><span class="token2">;</span><span class="token1">decrement</span><span class="token2">(</span><span class="token4">'votes'</span><span class="token2">,</span> <span class="token3">5</span><span class="token2">)</span><span class="token2">;</span>自增自减 <span class="token1">increment</span><span class="token2">(</span><span class="token4">'votes'</span><span class="token2">,</span> <span class="token3">1</span><span class="token2">,</span> <span class="token2">[</span><span class="token4">'name'</span> <span class="token">=</span><span class="token">></span> <span class="token4">'John'</span><span class="token2">]</span><span class="token2">)</span><span class="token2">;</span> 支持其他字段更新 DB<span class="token2">:</span><span class="token2">:</span><span class="token1">table</span><span class="token2">(</span><span class="token4">'users'</span><span class="token2">)</span><span class="token">-</span><span class="token">></span><span class="token1">truncate</span><span class="token2">(</span><span class="token2">)</span><span class="token2">;</span> 清空表 DB<span class="token2">:</span><span class="token2">:</span><span class="token1">table</span><span class="token2">(</span><span class="token4">'users'</span><span class="token2">)</span><span class="token">-</span><span class="token">></span><span class="token1">where</span><span class="token2">(</span><span class="token4">'votes'</span><span class="token2">,</span> <span class="token4">'>'</span><span class="token2">,</span> <span class="token3">100</span><span class="token2">)</span><span class="token">-</span><span class="token">></span><span class="token1">delete</span><span class="token2">(</span><span class="token2">)</span><span class="token2">;</span> 删除 $users <span class="token">=</span> DB<span class="token2">:</span><span class="token2">:</span><span class="token1">table</span><span class="token2">(</span><span class="token4">'users'</span><span class="token2">)</span><span class="token">-</span><span class="token">></span><span class="token1">paginate</span><span class="token2">(</span><span class="token3">15</span><span class="token2">)</span><span class="token2">;</span> 分页 $users <span class="token">=</span> DB<span class="token2">:</span><span class="token2">:</span><span class="token1">table</span><span class="token2">(</span><span class="token4">'users'</span><span class="token2">)</span><span class="token">-</span><span class="token">></span><span class="token1">simplePaginate</span><span class="token2">(</span><span class="token3">15</span><span class="token2">)</span><span class="token2">;</span> 简单分页只有上一页下一页 $results<span class="token">-</span><span class="token">></span><span class="token1">total</span><span class="token2">(</span><span class="token2">)</span> 总页数 <span class="token"><</span>div class<span class="token">=</span><span class="token4">"container"</span><span class="token">></span> @foreach <span class="token2">(</span>$users as $user<span class="token2">)</span> <span class="token2">{</span><span class="token2">{</span> $user<span class="token">-</span><span class="token">></span>name <span class="token2">}</span><span class="token2">}</span> @endforeach <span class="token"><</span><span class="token">/</span>div<span class="token">></span> <span class="token2">{</span><span class="token2">{</span> $users<span class="token">-</span><span class="token">></span><span class="token1">links</span><span class="token2">(</span><span class="token2">)</span> <span class="token2">}</span><span class="token2">}</span> ``` ``` - redis ``` <pre class="calibre16">``` use Illuminate\Support\Facades\Redis<span class="token2">;</span> Redis<span class="token2">:</span><span class="token2">:</span><span class="token1">get</span><span class="token2">(</span><span class="token4">'id'</span><span class="token2">)</span><span class="token2">;</span> Redis<span class="token2">:</span><span class="token2">:</span><span class="token1">set</span><span class="token2">(</span><span class="token4">'name'</span><span class="token2">,</span> <span class="token4">'Taylor'</span><span class="token2">)</span><span class="token2">;</span> Redis<span class="token2">:</span><span class="token2">:</span><span class="token1">command</span><span class="token2">(</span><span class="token4">'set'</span><span class="token2">,</span> <span class="token2">[</span><span class="token4">'name'</span><span class="token2">,</span><span class="token3">34</span><span class="token2">]</span><span class="token2">)</span><span class="token2">;</span> 支持所有原生 ``` ```