Laravel 链式调用 增删改查
控制器 home.php
<?php
namespace App\Http\Controllers;
use Illuminate\Support\Facades\DB;
use Illuminate\Http\Request;
class Home extends Controller
{
//查询多条记录
public function list(){
// echo '<pre>';
$res=DB::table('article')->get()->all();
// print_r($res);
return view('testobject',['result'=>$res]);
}
//查询多条记录,部分字段
public function list2(){
echo '<pre>';
$res=DB::table('article')->select('cate_id as CID','title as 标题')->where('cate_id','>',4)->get()->all();
print_r($res);
}
//like 查询
public function likes(){
// $res=DB::table('article')->where("title",'like','%版本%')->where('id',4)->get()->all();
$res=DB::table('article')->where("cate_id",4)->orWhere('cate_id',3)->get()->all();
return view('testobject',['result'=>$res]);
}
//where in 查询
public function wherein(){
$res=DB::table('article')->whereIn("id",[1,2,5])->get()->all();
// $res=DB::table('article')->whereIn("id",[1,2,5])->tosql();
// print($res);
return view('testobject',['result'=>$res]);
}
//join 连表查询
public function joins(){
// $res=DB::table('article')->join('users','users.id','=','article.uid')->get()->all();
$res=DB::table('article')->join('users','users.id','=','article.uid')
->select('article.id','article.uid','article.cate_id','users.name as nickname','article.title')
->get()->all();
echo '<pre>';
print_r($res);
}
//聚合 查询
public function pvs(){
// $res=DB::table('article')->get()->all();
// //原生计算平均值
// $avg=0;
// foreach ($res as $key=>$value){
// $avg+=$value->pv;
// }
// $avg=$avg/count($res);
// echo '<pre>';
// print_r($avg);
$res=DB::table('article')->avg('pv');
// $res=DB::table('article')->max('pv');
// $res=DB::table('article')->min('pv');
// $res=DB::table('article')->sum('pv');
// $res=DB::table('article')->count();
$res=(int)$res;
print_r($res);
}
//增加数据
public function insert2(){
$item1=['uid'=>1,'cate_id'=>5,'title'=>'测试插入多条insert语句','pv'=>200];
$item2=['uid'=>2,'cate_id'=>3,'title'=>'测试插入多条insert语句','pv'=>100];
$data[]=$item1;
$data[]=$item2;
$res=DB::table('article')->insert($data);
var_dump($res);
}
//增加数据并返回主键
public function insert3(){
$item=['uid'=>2,'cate_id'=>3,'title'=>'测试插入insert语句并返回id值','pv'=>50];
$res=DB::table('article')->insertGetId($item);
var_dump($res);
}
//修改数据
public function update2(){
$res=DB::table('article')->whereIn('id',[8,10])->update(array('title'=>'updta测试'));
var_dump($res);
}
//删除数据
public function delete2(){
$res=DB::table('article')->where('id',9)->delete();
var_dump($res);
}
}
路由 web.php
<?php
use Illuminate\Support\Facades\Route;
/*
|--------------------------------------------------------------------------
| Web Routes
|--------------------------------------------------------------------------
|
| Here is where you can register web routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| contains the "web" middleware group. Now create something great!
|
*/
Route::get('/', function () {
echo date("Y-m-d H:i:s");
return view('welcome');
});
//查询多条记录
Route::get('/dblist','home@list');
//查询多条记录,部分字段
Route::get('/dblist2','home@list2');
//like查询
Route::get('/dblikes','home@likes');
//where in 查询
Route::get('/dbwherein','home@wherein');
//join 连表 查询
Route::get('/dbjoins','home@joins');
// 聚合 计算查询
Route::get('/dbpvs','home@pvs');
//增加数据
Route::get('/dbinsert2','home@insert2');
//增加数据并返回主键
Route::get('/dbinsert3','home@insert3');
//修改数据
Route::get('/dbupdate2','home@update2');
//删除数据
Route::get('/dbdelete2','home@delete2');
视图 testobject.blade.php
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>数据库查询2</title>
<link rel="stylesheet" href="/layui/css/layui.css" />
<script src="/layui/layui.js"></script>
</head>
<body>
<!-- <?php
echo "<pre>";
print_r($result);?> -->
<table class="layui-table">
<thead>
<tr>
<td>ID</td>
<td>CATE_ID</td>
<td>标题</td>
</tr>
</thead>
<tbody>
<?php foreach($result as $val){?>
<tr>
<td><?php echo $val->id?></td>
<td><?php echo $val->cate_id?></td>
<td><?php echo $val->title?></td>
</tr>
<?php }?>
</tbody>
</table>
</body>
</html>
效果图如下:
总结:
1、Laravel 框架链式语法 容易理解便于记忆,关键词大多与php原生语法一致。开发效率较高。
2、条件语句中尽量用 “=”,效率高;避免用“>”或“<”,大数据量时速度很慢。在必须使用一个范围内查询时,推荐使用whereIn(‘字段’,[…])。
3、all()得出的数组,元素值是对象,可以通过 对象->属性名 调用;也可以通过array()将其转为数组,利用 数组[‘键名’]调用。