Laravel从更新查询中获取记录ID [英] Laravel Get Record ID from Update Query

查看:1433
本文介绍了Laravel从更新查询中获取记录ID的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对于此更新查询,我尝试在运行ID后获取ID.

For this update query, I'm trying to get the id after I run it.

$results = DB::table('testDB123.users')
    ->where('fbID', '=', $x['id'])
    ->update([
            'updated_at' => $x['currDate'],
            'fbFirstName' => $x['firstName'],
            'fbLastName' => $x['lastName']
        ]
    );

没有运气尝试过$results->id

是否有与insertGetId类似的更新查询?

Is there anything similar to insertGetId for update queries?

$id = DB::table($table1)->insertGetId([...])

推荐答案

update()方法不返回对象,因此有两个选择:

update() method doesn't return an object, so you have two options:

选项1

使用updateOrCreate():

$user = User::updateOrCreate(['fbID' => $x['id']], $dataArray);
$id = $user->id;

选项2

获取对象并对其进行更新:

Get an object and update it:

$user = User::where('fbID', '=', $x['id'])->first();
$user->update($dataArray);

$id = $user->id;

这篇关于Laravel从更新查询中获取记录ID的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆