返回操作stu表C......登陆

操作stu表CURD操作

JasonKim2019-05-14 21:47:02238
<?php

namespace App\Http\Controllers\home;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\DB;

class StuController extends Controller
{
    // 主函数调用
    public function main()
    {
        // 查询操作
        $res = $this->select();
        dump($res);

        // 写入操作
        $res = $this->insert();
        dump($res);
        
        // 更新操作
        $res = $this->update();
        dump($res);

        //删除操作
        $res = $this->del();
        dump($res);
    }

    // 查询操作
    private function select()
    {
        // 获取指定ID的一条数据
        $res = DB::table('stu')->find(1);
        // 获取随机的一条记录
        $res = DB::table('stu')->inRandomOrder()->first();
        // 获取所有数据
        $res = DB::table('stu')->get();
        //dump($res);
        return $res;
    }

    // 写入操作
    private function insert()
    {
        // 写入并返回自增ID
        $res = DB::table('stu')->insertGetId([
            'name'=>'杰森斯坦森',
            'age' =>33,
            'salary'=>6768,
            'create_time'=>time(),
            'update_time'=>time()
        ]);
        //dump($res);
        return $res;
    }

    // 更新操作
    private function update($id=7)
    {
        $res = DB::table('stu')->where('id',$id)->update([
            'salary'=>8867
        ]);
        return $res;
    }

    // 删除操作
    private function del($id=7)
    {
        $res = DB::table('stu')->where('id',$id)->delete();
        return $res;
    }
}


最新手记推荐

• 用composer安装thinkphp框架的步骤• 省市区接口说明• 用thinkphp,后台新增栏目• 管理员添加编辑删除• 管理员添加编辑删除

全部回复(0)我要回复

暂无评论~
  • 取消回复发送