thinkphp6返回插入数据id

文章描述:

thinkphp插入数据并且返回插入信息的id

insert

Db::name('article')->insert([
            'title'    => '123',//标题
            'content'  => '123456',//内容
            'time' => date('Y-m-d H:i:s',time()),//添加时间
        ], false, false,'news_insert_id');
echo $newID = Db::name('article')->getLastInsID('news_insert_id');

 

// 推荐 插入并返回自增ID
echo $id = Db::name('order')->insertGetId($data);

tp6默认主键为id,如果你没有使用id作为主键名,需要在模型中设置组件的字段属性:

namespace app\model;
use think\Model;

class Order extends Model
{
    protected $pk = 'order_id';
}

 

发布时间:2023/04/26

发表评论