public function test(){
$test_data=Test::all()->toArray();
return view(
'test/form',[
'data'=>
$test_data]);
}
public function test1(){
$data=Request::all();
$data[
'username'];
$num=DB::table(
'test')->insert([
[
'username'=>
$data[
'username'],
'title'=>
$data[
'title'],
'content'=>
$data[
'content'],
'addtime'=>date(
'Y-m-d H:i:s',time())]
]);
if(
$num){
$test_data=Test::all()->toArray();
return view(
'test/form',[
'data'=>
$test_data]);
}
}
public function del($id){
$num=DB::table(
'test')->where(
'id',
$id)->delete();
if(
$num){
echo "1";
}
else{
echo "0";
}
}
//综合
public
function query1(){
//添加
/*$bool=
DB::table
('test')->insert([
[
'username'=>'test1',
'title'=>'test1234'],
[
'username'=>'test2',
'title'=>'test12345'],
[
'username'=>'test3',
'title'=>'test12345'],
[
'username'=>'test4',
'title'=>'test12345'],
[
'username'=>'test5',
'title'=>'test12345'],
]);*/
/*$bool=DB::table('test')->insertGetId(['username'=>'test123','title'=>'test123']);*/
//print_r($bool);
//修改
/*$num =
DB::table
('test')
->where
(['id'=>1])
->update([
'username'=>'username']);*/
//自增 自减
//$num =
DB::table
('test')->where
(['id'=>1])->increment(
'title');
//默认是加
1
/*$num =
DB::table
('test')
->where
(['id'=>1])
->decrement(
'title',
12,[
'username'=>'testtestygbaefj']);
//默认是减
1 修改*/
//删除
//$num =
DB::table
('test')->where
('id',1)->delete();
//$num =
DB::table
('test')->where
('id','>=',3)->delete();
//$num =
DB::table
('test')->truncate();
//var_dump($num );die;
//查询
//$num =
DB::table
('test')->where
('id','>','2')->orderBy
('id','desc')->get();
//查询多条数据
//$num =
DB::table
('test')->where
('id','>',2)->first();
//查询一条数据
//多条件查询
//$num =
DB::table
('test')->whereRaw
('id >? and username=?',['2','test3'])->get();
//返回结果集中指定的字段pluck list select
//$name =
DB::table
('test')->where
('id','4')->pluck(
'username');
//$name =
DB::table
('test')->lists(
'username',
'id');
//$name =
DB::table
('test')->limit
(3)->select
('username','id','title')->get();
//多字段查询
//chunk 拆分
/*$name =
DB::table
('test')->chunk(
2,
function($stutent){
print_r($stutent);
return false;
//if(){
//return false;终止
//}
});*/
//聚合函数 count max min avg sum
//$num =
DB::table
('test')->count();
//$num =
DB::table
('test')->max(
'id');
//$num =
DB::table
('test')->min(
'id');
//$num =
DB::table
('test')->avg(
'id');
//$num =
DB::table
('test')->sum(
'id');
//dd($num);
//return "fhasdpofaspofjpa";
}
//查询构造器
public
function rom(){
//查询
//$test=
Test::all();
//查询所有记录
//$test =
Test::find(
1);
//查询
1条记录
//$test =
Test::findOrFail(
1);
//查询
1条记录
//$test=
Test::get();
//$test=
Test::where
('id','>','2')->orderBy
('id','desc')->first();
// Test::chunk(
2,
function($test){dd($test); });
//dd($test);
//$num=
Test::where
('id','>',2)->count();
//$num =
Test::where
('id','>',2)->max(
'id');
//添加
$test =
new Test();
$test->username=
'test2';
$test->title=
'test2';
$test->content=
'test2';
$data=$test->save();
$test=
Test::find(
6);
$update_at=$test->created_at;
echo $update_at;
//print_r(date(
'Y-m-d H:i:s',$test->created_at));
}
转载请注明原文地址: https://ju.6miu.com/read-36183.html