Home > Backend Development > PHP Tutorial > After successfully saving the model, how to obtain the primary key of this model?

After successfully saving the model, how to obtain the primary key of this model?

WBOY
Release: 2023-03-01 21:52:01
Original
1173 people have browsed it

How to return the saved() model primary key after model save()?

Reply content:

How to return the saved() model primary key after model save()?

Just getId() (if your primary key is called id)

<code class="php">$user = new User;
$user->name='sf';

$user->save();
dump($user->id);//直接取id主键
</code>
Copy after login

<code>$user->save();
// 获取自增ID
echo $user->id;</code>
Copy after login
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template