zoukankan      html  css  js  c++  java
  • 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);
        }
  • 相关阅读:
    php转义和去掉html、php标签函数
    php命令行模式
    php开启新的进程或者线程
    防止便秘的食物
    各种米的营养价值
    select option jquery javascript
    mysql datetime、date、time、timestamp区别
    五脏之对应体液志窍时
    Html简单demo_html列表中进行编辑操作
    mysql sql语句使用技巧
  • 原文地址:https://www.cnblogs.com/lx0715/p/10058098.html
Copyright © 2011-2022 走看看