一、数据库:DB 1、db查 DB::table('msg')->where('id','>',$id)->get() 查询单行 DB::table('msg')->where('id','=',$id)->first() 2、db增 DB::table('msg')->insertGetId($data);//返回插入的行号 DB::table('msg')->insert($data);//返回执行插入的结果 布尔值 3、db删 DB::table('msg')->where('id','=',$id)->delete(); 或 DB::table('msg')->where('id',$id)->delete(); 4、db改 DB::table('msg')->where('id',$id)->update($data); 二、模型:model 1、注意 a.laravel系统默认 模型名称和数据表有一定关联的 也就是数据表和模型名相差一个s (即表名是users那么模型类名是user) 假如数据表是user,模型的类名也是user怎么办 为了解决这个问题需要在模型里面配置 public $table = 'user'; 2、模型增 用$model->property;$model->save() b.当数据表中没有update_at 和created_at字段时(Eloquent 在数据的表中自动地将维护 created_at 和 updated_at 字段) 使用$model->save()会出现如下错误 Column not found: 1054 Unknown column 'updated_at' in 解决方案: i:只需简单的添加这些 timestamp 字段到表中。 ii:如果您不希望 Eloquent 维护这些字段,在模型中添加以下属性: public $timestamps = false; 3、模型查 单行 用(new Msg())->find($id); 用Msg::find($id); 多行 Msg::all('id','title','content')//查询多行 并查询其中某些字段 Msg::where('id','<',$id)->get(['id','title']) Msg::where('id','<',$id)->select('id','title')->get() 4、模型改 $msg = Msg::find($id); $msg->title = $_POST['title']; $msg->content = $_POST['content']; $msg->save(); 5、模型删 $msg = Msg::find($id); $msg->delete(); 或 Msg::where('id',$id)->delete(); 6、复杂查询 a.排序 限制order limit Msg::orderBy('id','desc')->skip(2)->take(5)->get()//skip跳过2条,take拿5条相当于 limit(2,5) b.统计等 count Msg::count('*');Msg::count(); Msg::max('id'); c.分组 Goods::select(DB::row('avg(shop_price)'))->groupBy('cat_id')->get() //因为select(avg(shop_price))里面的参数 //会被laravel系统理解为数据表的字段 //所以要用DB::row('avg(shop_price)') 7、model约定 laravel默认 protected $table = 'msgs'; protected $timestamps = true; protected $primaryKey = 'id'; 在model里面可以修改 protected $table = 'msg'; protected $timestamps = false; protected $primaryKey = 'xx_id'; 三、request 对象(有点像tp的i函数) 更新数据 function update(Request $req,$id){ //然后所有请求信息可以从$req中获取 $msg = Msg::find($id); $msg->title = $req->title; 或 $msg->title = $req->input('title','如果没有post[title],这里是默认值'); ...... } 上传文件 $req->file('pic')->move('./images/','test.jpg');