• laravel-9-laravel数据库查询 查询组件


    laravel数据库查询

    首先 先添加路由

    Route::prefix('datebase')->group(function() {
        Route::get('insert', "DatebaseController@insert");
        Route::get('get', "DatebaseController@get");
    });

    get路由为获取数据的方法

    再在对应的控制器中添加方法

    /**
         *
         *查询数据
         */
        public function get() 
        {
            // 查询表中所有
            $data = DB::table('articles')->get();
            dump($data);
            // 条件查询
            $data1 = DB::table('articles')
            // id=1
            // ->where('id', 1)
            // 不包含id为2 
            // ->where('id', '<>', 2)
            // id >1的
            // ->where('id', '>', 1)
            // id <4的
            // ->where('id', '<', 4)
            // 取id为 1,3,5。 IN
            // ->whereIn('id',[1, 3, 5])
            //notin 取id除为1.3.5之外剩下的
            // ->whereNotIn('id', [1, 3, 5])
            // between 取一段之内的数据
            ->whereBetween('id', [2,4])
            // 排序
            ->orderBy('id','desc')
            //limit 限制查询条数
            // ->limit(2)
            // 等等 其他方法详见 laravel手册
            // ->get();
            //first() 只获取一条数据
            // ->first();
            // pluck()只获取某个字段的时候,两个参数,第一个要获取的字段名,第二个可选 用来做key
            // ->pluck('content', 'title');
            // value()获取一个值
            ->value('title');    
            dump($data1);
    
    
            $data2 = DB::table('articles')
                ->select('category_id', 'title', 'content')
                ->where('title', '<>', "'文章1'")
                ->whereIn('id', [1, 2, 3])
                ->groupBy('id')
                ->orderBy('id', 'desc')
                ->limit(1)
                ->get();
            dump($data2);
        }
  • 相关阅读:
    第五章总结
    第二章总结
    第一章、基础知识总结
    实验 9 根据材料编程
    实验5
    汇编实验4
    实验 3 编程、编译、连接、跟踪
    实验 2 用机器指令和汇编指令编程
    汇编-实验一
    react面试笔录
  • 原文地址:https://www.cnblogs.com/lx0715/p/10058098.html
Copyright © 2020-2023  润新知