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); }