返回通过model......登陆

通过model 实现增删改查及软删除

关志宇2019-07-04 13:18:55190

controller/book.php

<?php
/**
* Created by PhpStorm.
* User: Administrator
* Date: 2019/7/4
* Time: 12:28
*/

namespace app\index\controller;

use think\Db;
use think\Controller;
use app\index\model\Book as BookModel;
class Book extends Controller
{
   //添加
   public function add(){
       $book = new BookModel;
       $book->bookname = 'php中文网11111';
       $book->bookprice = '120.1';
       $book->status = '2';
       $book->bookcount=100;
       $book->save();
   }
   //修改
   public function update(){
       $book = BookModel::get(1);
       $book->bookname = '中文网';
       $book->bookprice = 123.01;
       $book->bookcount = 100;
       $book->status = 2;
       $book->save();
   }
   //查询
   public function select(){
       $book = BookModel::get(1);
       dump($book);
   }
   //删除
   public function del(){
       $book = BookModel::get(1);
       $book->delete();
   }

   //软删除
   public function softDelete(){
       BookModel::destroy(1);
   }
}

#/model/Book.php

<?php
/**
* Created by PhpStorm.
* User: Administrator
* Date: 2019/7/4
* Time: 12:30
*/

namespace app\index\model;


use think\Model;
use think\model\concern\SoftDelete;
class Book extends Model
{
   use SoftDelete;
   protected $deleteTime = 'delete_time';
   protected $defaultSoftDelete = 0;
}

最新手记推荐

• 用composer安装thinkphp框架的步骤• 省市区接口说明• 用thinkphp,后台新增栏目• 管理员添加编辑删除• 管理员添加编辑删除

全部回复(0)我要回复

暂无评论~
  • 取消回复发送
  • PHP中文网