abstract:<?php namespace App\Http\Controllers\home; use App\models\StuModel; use Illuminate\Http\Request; use App\Http\Controllers\Controller; use Illuminate\Support\Facades\DB;
<?php namespace App\Http\Controllers\home; use App\models\StuModel; use Illuminate\Http\Request; use App\Http\Controllers\Controller; use Illuminate\Support\Facades\DB; class StuController extends Controller { // 主函数调用 public function main($type) { switch ($type) { case 'select': // 查询操作 $res = $this->select(); dump($res); break; case 'insert': // 写入操作 $res = $this->insert(); dump($res); break; case 'update': // 更新操作 $res = $this->update(15); dump($res); break; case 'del': //删除操作 $res = $this->del(14); dump($res); break; } } // 查询操作 private function select() { // 获取指定ID的一条数据 /*$res = DB::table('stu')->find(1); // 获取随机的一条记录 $res = DB::table('stu')->inRandomOrder()->first(); // 获取所有数据 $res = DB::table('stu')->get();*/ //dump($res); // 使用模型操作获取一条数据 $res = StuModel::find(1); // 获取多条记录 $res = StuModel::get(); // 获取指定字段 $res = StuModel::select(['name','age'])->get(); return $res; } // 写入操作 private function insert() { // 写入并返回自增ID /*$res = DB::table('stu')->insertGetId([ 'name'=>'杰森斯坦森', 'age' =>33, 'salary'=>6768, 'create_time'=>time(), 'update_time'=>time() ]);*/ //dump($res); $stu = new StuModel(); $stu->name = '扫地僧'; $stu->age = 45; $stu->salary = 100090; // 执行保存操作,新增 $stu->save(); return $stu; } // 更新操作 private function update($id=7) { /*$res = DB::table('stu')->where('id',$id)->update([ 'salary'=>8867 ]);*/ // 使用模型更新操作 $stu = StuModel::find($id); $stu->salary = 9889; $stu->save(); return '更新成功'; } // 删除操作 private function del($id=7) { // 使用查询构造器操作 /*$res = DB::table('stu')->where('id',$id)->delete(); return $res;*/ // 使用模型操作删除 StuModel::where('id',$id)->delete(); return '删除成功'; } }
Correcting teacher:查无此人Correction time:2019-05-16 09:33:51
Teacher's summary:完成的不错。现在大多框架,模式都一样。继续加油