Laravel学习笔记基础系列--(二十四)Laravel 模型CURD快速使用

作者: 温新

分类: 【Laravel】

阅读: 1950

时间: 2021-07-25 15:14:06

作者:温新

时间:2021-06-30

hi,我是温新,一名PHPer

1、Laravel提供了ORM对数据库的快速操作。使用ORM必须要有对应的模型文件,一个模型文件对应一个数据表。

2、Laravel模型使用了约定优于配置,要把模型操作使用好,那么就有必要了解。如模型约定了表中的主键字段必须是id;使用迁移文件创建一个表时,会为表自动添加一个s。如新建post表,Laravel为自动为其添加s,也就变成了posts表。关于约定配置,可以通过模型属性进行自定义设置。

3、所有DB中的方法,模型中均可使用。

4、本文使用posts表进行演示。

准备工作

创建post模型类

php artisan make:model Post

模型属性

数据库

protected $connection = 'connection-name';指定操作的数据库。

表名

protected $table = '自定义表名';,如protected $table = 'posts';

通过table属性,可以任意指定表名。

主键

protected $primaryKey = '主键字段';,如protected $primaryKey = 'uuid';

默认使用的主键是id,很多项目中的主键使用的是uuid或自定义的主键,就可通过该属性设置。

时间戳

public $timestamps = false;

模型希望第个表中都有created_atupdated_at时间字段存于表中,添加与修改时会自动更新对应的时间戳。如表中没有这两个字段,将该属性设置为flase

格式化时间

protected $dateFormat = 'U'

模型默认存储时间格式为Y-m-d H:i:s,如要使用时间戳存入数据表,那么将该属性值设置为U即可。

白名单

protected $fillable = [];。允许操作表中的哪些字段,可以被批量赋值的字段。

黑名单

protected $guarded = [],若所有字段都可以批量赋值,该属性设置为空数组。

新增数据

由于批量操作需要设置白名单或黑名单,因此我们就来设置一下,需要使用批量赋值时就不用再设置了。

<span style="box-sizing: border-box;padding-right: 0.1px"><span style="box-sizing: border-box;color: rgb(170, 85, 0)">// app/Models/Post</span></span><br></br><span style="box-sizing: border-box;padding-right: 0.1px"><span style="box-sizing: border-box;color: rgb(119, 0, 136)">protected</span> <span style="box-sizing: border-box;color: rgb(0, 85, 170)">$guarded</span> <span style="box-sizing: border-box;color: rgb(152, 26, 26)">=</span> [];</span>

save()方法-新增一条数据

<span style="box-sizing: border-box;padding-right: 0.1px"><span style="box-sizing: border-box;color: rgb(170, 85, 0)">// DemoController中的demo方法</span></span><br></br><span style="box-sizing: border-box;padding-right: 0.1px"><span style="box-sizing: border-box;color: rgb(119, 0, 136)">use</span> <span style="box-sizing: border-box;color: rgb(0, 0, 0)">App\Models\Post</span>;</span><br></br><span style="box-sizing: border-box;padding-right: 0.1px"><span style="box-sizing: border-box;color: rgb(119, 0, 136)">use</span> <span style="box-sizing: border-box;color: rgb(0, 0, 0)">Illuminate\Http\Request</span>;</span><br></br><span style="box-sizing: border-box;padding-right: 0.1px"><span style="box-sizing: border-box;color: rgb(119, 0, 136)">public</span> <span style="box-sizing: border-box;color: rgb(119, 0, 136)">function</span> <span style="box-sizing: border-box;color: rgb(0, 0, 255)">demo</span>(<span style="box-sizing: border-box;color: rgb(0, 0, 0)">Request</span> <span style="box-sizing: border-box;color: rgb(0, 85, 170)">$request</span>)</span><br></br><span style="box-sizing: border-box;padding-right: 0.1px">{</span><br></br><span style="box-sizing: border-box;padding-right: 0.1px">    <span style="box-sizing: border-box;color: rgb(0, 85, 170)">$post</span>          <span style="box-sizing: border-box;color: rgb(152, 26, 26)">=</span> <span style="box-sizing: border-box;color: rgb(119, 0, 136)">new</span> <span style="box-sizing: border-box;color: rgb(0, 0, 0)">Post</span>();</span><br></br><span style="box-sizing: border-box;padding-right: 0.1px">    <span style="box-sizing: border-box;color: rgb(0, 85, 170)">$post</span> <span style="box-sizing: border-box;color: rgb(152, 26, 26)">-></span><span style="box-sizing: border-box;color: rgb(0, 0, 0)">title</span>  <span style="box-sizing: border-box;color: rgb(152, 26, 26)">=</span> <span style="box-sizing: border-box;color: rgb(170, 17, 17)">'我是标题'</span>;</span><br></br><span style="box-sizing: border-box;padding-right: 0.1px">    <span style="box-sizing: border-box;color: rgb(0, 85, 170)">$post</span><span style="box-sizing: border-box;color: rgb(152, 26, 26)">-></span><span style="box-sizing: border-box;color: rgb(0, 0, 0)">content</span> <span style="box-sizing: border-box;color: rgb(152, 26, 26)">=</span> <span style="box-sizing: border-box;color: rgb(170, 17, 17)">'我是内容'</span>;</span><br></br><span style="box-sizing: border-box;padding-right: 0.1px">    <span style="box-sizing: border-box;color: rgb(0, 85, 170)">$post</span><span style="box-sizing: border-box;color: rgb(152, 26, 26)">-></span><span style="box-sizing: border-box;color: rgb(0, 0, 0)">user_id</span> <span style="box-sizing: border-box;color: rgb(152, 26, 26)">=</span> <span style="box-sizing: border-box;color: rgb(17, 102, 68)">1</span>;</span><br></br><span style="box-sizing: border-box;padding-right: 0.1px">    <span style="box-sizing: border-box;color: rgb(0, 85, 170)">$result</span>        <span style="box-sizing: border-box;color: rgb(152, 26, 26)">=</span><span style="box-sizing: border-box;color: rgb(0, 85, 170)">$post</span><span style="box-sizing: border-box;color: rgb(152, 26, 26)">-></span><span style="box-sizing: border-box;color: rgb(0, 0, 0)">save</span>();</span><br></br><span style="box-sizing: border-box;padding-right: 0.1px"><span style="box-sizing: border-box"></span></span><br></br><span style="box-sizing: border-box;padding-right: 0.1px">    <span style="box-sizing: border-box;color: rgb(119, 0, 136)">return</span> <span style="box-sizing: border-box;color: rgb(0, 0, 0)">view</span>(<span style="box-sizing: border-box;color: rgb(170, 17, 17)">'demo.demo'</span>);</span><br></br><span style="box-sizing: border-box;padding-right: 0.1px">}</span>

打开数据表会发现,created_atupdated_at都被自动添加了时间。

create()-批量赋值(用的很多)

金条数据的添加一定要设置白名单或黑名单。

<span style="box-sizing: border-box;padding-right: 0.1px"><span style="box-sizing: border-box;color: rgb(170, 85, 0)">// 成功,返回该数据模型对象</span></span><br></br><span style="box-sizing: border-box;padding-right: 0.1px"><span style="box-sizing: border-box;color: rgb(0, 85, 170)">$posts</span> <span style="box-sizing: border-box;color: rgb(152, 26, 26)">=</span> <span style="box-sizing: border-box;color: rgb(0, 0, 0)">Post</span>::<span style="box-sizing: border-box;color: rgb(0, 0, 0)">create</span>([</span><br></br><span style="box-sizing: border-box;padding-right: 0.1px">    <span style="box-sizing: border-box;color: rgb(170, 17, 17)">'title'</span> <span style="box-sizing: border-box;color: rgb(152, 26, 26)">=></span>  <span style="box-sizing: border-box;color: rgb(170, 17, 17)">'我是标题1'</span>,</span><br></br><span style="box-sizing: border-box;padding-right: 0.1px">    <span style="box-sizing: border-box;color: rgb(170, 17, 17)">'content'</span><span style="box-sizing: border-box;color: rgb(152, 26, 26)">=></span> <span style="box-sizing: border-box;color: rgb(170, 17, 17)">'我是内容1'</span>,</span><br></br><span style="box-sizing: border-box;padding-right: 0.1px">    <span style="box-sizing: border-box;color: rgb(170, 17, 17)">'user_id'</span><span style="box-sizing: border-box;color: rgb(152, 26, 26)">=></span> <span style="box-sizing: border-box;color: rgb(17, 102, 68)">1</span>,</span><br></br><span style="box-sizing: border-box;padding-right: 0.1px">]);</span>

需要注意的是,模型的添加操作,无法一次性插入多条数据。

firstOrCreate-查找添加,有则更新,无则添加

<span style="box-sizing: border-box;padding-right: 0.1px"><span style="box-sizing: border-box;color: rgb(170, 85, 0)">// 添加操作</span></span><br></br><span style="box-sizing: border-box;padding-right: 0.1px"><span style="box-sizing: border-box;color: rgb(0, 85, 170)">$posts</span> <span style="box-sizing: border-box;color: rgb(152, 26, 26)">=</span> <span style="box-sizing: border-box;color: rgb(0, 0, 0)">Post</span>::<span style="box-sizing: border-box;color: rgb(0, 0, 0)">firstOrCreate</span>([<span style="box-sizing: border-box;color: rgb(170, 17, 17)">'title'</span><span style="box-sizing: border-box;color: rgb(152, 26, 26)">=></span><span style="box-sizing: border-box;color: rgb(170, 17, 17)">'我是温新'</span>,<span style="box-sizing: border-box;color: rgb(170, 17, 17)">'user_id'</span><span style="box-sizing: border-box;color: rgb(152, 26, 26)">=></span><span style="box-sizing: border-box;color: rgb(17, 102, 68)">21</span>,<span style="box-sizing: border-box;color: rgb(170, 17, 17)">'content'</span><span style="box-sizing: border-box;color: rgb(152, 26, 26)">=></span><span style="box-sizing: border-box;color: rgb(170, 17, 17)">'Laravel笔记'</span>]);</span>

firstOrCreate先去匹配给定的列值是否存在,若没有则添加。

updateOrCreate-存在则更新,不存在则添加

$posts = Post::updateOrCreate(['id'=>106],['user_id'=>2,'content'=>'laravel笔记被更新了']);

通过id查找数据,找到了则更新,没有找到则添加。

修改数据

save()-修改数据

添加和修改都是save,不同的是,若要修改,则必须先查询对应的数据,然后通过更新属性值进行数据的更新。

<span style="box-sizing: border-box;padding-right: 0.1px"><span style="box-sizing: border-box;color: rgb(170, 85, 0)">// 成功返回true</span></span><br></br><span style="box-sizing: border-box;padding-right: 0.1px"><span style="box-sizing: border-box;color: rgb(0, 85, 170)">$posts</span> <span style="box-sizing: border-box;color: rgb(152, 26, 26)">=</span> <span style="box-sizing: border-box;color: rgb(0, 0, 0)">Post</span>::<span style="box-sizing: border-box;color: rgb(0, 0, 0)">find</span>(<span style="box-sizing: border-box;color: rgb(17, 102, 68)">106</span>);</span><br></br><span style="box-sizing: border-box;padding-right: 0.1px"><span style="box-sizing: border-box;color: rgb(0, 85, 170)">$posts</span><span style="box-sizing: border-box;color: rgb(152, 26, 26)">-></span><span style="box-sizing: border-box;color: rgb(0, 0, 0)">title</span> <span style="box-sizing: border-box;color: rgb(152, 26, 26)">=</span> <span style="box-sizing: border-box;color: rgb(170, 17, 17)">'hello laravel8.x'</span>;</span><br></br><span style="box-sizing: border-box;padding-right: 0.1px"><span style="box-sizing: border-box;color: rgb(0, 85, 170)">$result</span> <span style="box-sizing: border-box;color: rgb(152, 26, 26)">=</span> <span style="box-sizing: border-box;color: rgb(0, 85, 170)">$posts</span><span style="box-sizing: border-box;color: rgb(152, 26, 26)">-></span><span style="box-sizing: border-box;color: rgb(0, 0, 0)">save</span>();</span>

update-批量更新

Post::where('id','>',100)->update(['content'=>'哦豁,一样的内容了']);

删除

delete--方法

// 删除一条
$post = Post::find(106);
$post->delete();

destroy--方法

// 删除一条
$post = Post::destroy(105);
// 删除多条
$post = Post::destroy([101,102,103,104]);

软件删除

软删除需要有一个deleted_at字段,请自行通过迁移文件添加该字段。该字段默认为NULL,数据被删除时,会记录当前时间,用于说明该数据被删除。

// 1
php artisan make:migration add_deleted_at_to_posts --table=posts
// 2
public function up()
{
    Schema::table('posts', function (Blueprint $table) {
        $table->softDeletes();
    });
}
public function down()
{
    Schema::table('posts', function (Blueprint $table) {
        $table->dropSoftDeletes();
    });
}
// 3
php artisan migrate --path=database/migrations/2021_06_30_160851_add_deleted_at_to_posts.php
// 4、Post模型文件
use Illuminate\Database\Eloquent\SoftDeletes;
class Post extends Model
{
    use SoftDeletes;
}

需要注意的是,需要在Post模型文件中使用use SoftDeletes;

删除数据

$post = Post::destroy(100);

实际上数据并被没有删除,而是将deleted_at写入了删除的时间,用以标志删除。

withTrashed()-只获取软删除的数据

$posts = Post::onlyTrashed()->get();

restore()-恢复被软删除的数据

 $post = Post::onlyTrashed()->restore();

forceDelete()-永久删除

$post = Post::onlyTrashed()->forceDelete();

查询

all()查询所有数据

$posts = Post::all();

first() 查询一条数据

$post = Post::first();

find() 获取单个模型结果

$post = Post::find(10);

findOrFail() 查询不到抛出404页面

$posts = Post::findOrFail(110);

关于模型事件操作,会放在进阶篇中进行记录。

我是温新

每天进步一点点,就一点点

请登录后再评论