push
一对一关联模型,使用push可以一起保存。
$dynamic = PersonDynamic::query()->where('id', 1)->first();
$dynamic->setAttribute('content', '哈哈哈');
$dynamic->user->money = 10;
$dynamic->push();
不触发模型事件
laravel5.7以上
YourModel::withoutEvents(function(){
// do something...
});
laravel5.7以下
$dispatcher = PersonDynamic::getEventDispatcher();
PersonDynamic::unsetEventDispatcher();
$first = PersonDynamic::query()->where('id', 1)->first();
$first->content = 999;
$first->saveOrFail();
PersonDynamic::setEventDispatcher($dispatcher);
模型是否被修改过
$first = PersonDynamic::query()->where('id', 1)->first();
var_dump($first->isDirty());//bool(false)
$first->setAttribute('content', 'haha');
var_dump($first->isDirty());//bool(true)
var_dump($first->isDirty(['content']));//bool(true)
var_dump($first->isDirty(['user_id']));//bool(false)
var_dump($first->getDirty());//["content" => "haha"]
var_dump($first->getOriginal());//原始数据
var_dump($first->getOriginal('content'));//745646
TimorQaQ 发布了14 篇原创文章 · 获赞 1 · 访问量 847 私信 关注