bailangsyc 2020-02-20
CREATE TABLE `user` ( `id` int(11) NOT NULL AUTO_INCREMENT, `username` varchar(255) NOT NULL, `password` varchar(255) NOT NULL, `created_at` timestamp NULL DEFAULT NULL ON UPDATE CURRENT_TIMESTAMP, `updated_at` timestamp NULL DEFAULT NULL ON UPDATE CURRENT_TIMESTAMP, PRIMARY KEY (`id`) ) ENGINE=InnoDB AUTO_INCREMENT=14 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci;
Route::group([‘prefix‘=>‘user‘],function (){ Route::get(‘/add‘,‘‘);//添加 Route::get(‘/delete‘,‘‘);//删除 Route::get(‘/update‘,‘‘);//修改 Route::get(‘/query‘,‘‘);//查询 });
<?php namespace App\Models; use Illuminate\Database\Eloquent\Model; class User extends Model { //添加表名 protected $table = ‘user‘; //设置之后允许用户批量操作 protected $fillable = [‘username‘, ‘password‘]; //禁用时间戳 public $timestamps = false; }
<?php namespace App\Http\Controllers; use App\Models\User; use Illuminate\Http\Request; class UserController extends Controller { //orm添加方法 public function add() { $data = User::create([‘username‘ => ‘胡歌‘, ‘password‘ => ‘222‘]); return dd($data); } //删除方法 public function delete() { $data = User::find(13)->delete(); return dd($data); } //修改方法 public function update() { $data = User::where([‘id‘ => ‘10‘])->update([‘username‘ => ‘实验‘]); return dd($data); } //查询方法 public function query() { $data = User::all(); return dd($data); } }