简体   繁体   English

如何在Laravel DB :: insert中返回我的OUTPUT子句

[英]How do I return my OUTPUT clause in Laravel DB::insert

I am using Laravel and sqlsrv, connected to SQL Server 2016 and all is working great until I try to use an output clause in my insert query. 我正在使用Laravel和sqlsrv,连接到SQL Server 2016,所有工作都很好,直到我尝试在插入查询中使用输出子句。

Query is something like 查询是这样的

INSERT INTO TABLE(Columns) OUTPUT INSERTED.MyDesiredReturnColumn VALUES(Value)

This is working perfectly in SQL Server, and returning the desired value, but using Laravel's DB::insert functionality it is only returning a 1 (for successful insert) 这在SQL Server中完美地工作,并返回所需的值,但是使用Laravel的DB :: insert功能它只返回1(成功插入)

I have a workaround that I would rather not have right now, using the CreatedOn field to return the most recently created row, but this has potential issues. 我有一个我现在不想拥有的解决方法,使用CreatedOn字段返回最近创建的行,但这有潜在的问题。

UPDATES: The field I am attempting to retrieve is a uniqueidentifier field (guid) that is created in SQL, not from Laravel-side 更新:我试图检索的字段是一个在SQL中创建的uniqueidentifier字段(guid),而不是来自Laravel端

After attempting @PrathameshPalav's recommendation of using the Eloquent model creation, the values are being inserted correctly into the DB, but it is not returning the uniqueidentifier still. 尝试@ PrathameshPalav建议使用Eloquent模型创建后,值正在正确插入数据库,但它仍然没有返回uniqueidentifier。

$inserted = MyModel::create($information);
print "inserted id is " . $inserted->MyModelId;

This is printing "inserted id is " 这是打印“插入ID是”

Here is my model: 这是我的模型:

namespace App;
use Illuminate\Database\Eloquent\Model;    
class MyModel extends Model
{
    //
    protected $table = 'MyModelBase';
    protected $primaryKey = 'MyModelId';
    public $incrementing = false;
    protected $keyType = "string";
    public $timestamps = false;
    protected $fillable = ['field1', 'field2', 'etc'];
}

Any ideas would be greatly helpful. 任何想法都会有很大帮助。

You can use Eloquent ORM for this purpose: 您可以将Eloquent ORM用于此目的:

$insertedObject = ModelName::create($input_array);

It will return inserted model object in response. 它将返回插入的模型对象作为响应。 Or if you want only inserted record id then use 或者,如果您只想插入记录ID,则使用

DB::table($tablename)->insertGetId($input_array);

The way that I solved this was by incorporating an Eloquent model (as pointed out by @PrathameshPalav), then (loosely) following this tutorial https://danielkoch.work/log/laravels-eloquent-guids.html 我解决这个问题的方法是通过合并一个Eloquent模型(由@PrathameshPalav指出),然后(松散地)遵循本教程https://danielkoch.work/log/laravels-eloquent-guids.html

Specifically this part 特别是这一部分

public static function boot() {
    parent::boot();

    // Hook when a model is created
    static::creating(function ($model) {
        // Select a new ID
        $result = DB::select( DB::raw('Select NewID() NewUUID') );

        $model->{$model->getKeyName()} = $result[0]->NewUUID;
    });
}

After that, I added the primary key I had defined to the $fillable array and tested, and it works =) 之后,我将我定义的主键添加到$fillable数组并进行测试,它的工作原理=)

Thank you both for your help! 谢谢你们的帮助!

Yes, when you use insert it will return a bool. 是的,当你使用插入时,它会返回一个bool。 You can use insertGetId to get the the id. 您可以使用insertGetId来获取id。

If the table has an auto-incrementing id, use the insertGetId method to insert a record and then retrieve the ID: 如果表具有自动递增ID,请使用insertGetId方法插入记录,然后检索ID:

$data = [....]; // data that will be inserted
$id = DB::table('xxx')->insertGetId($data); 

More info: https://laravel.com/docs/5.5/queries#inserts 更多信息: https//laravel.com/docs/5.5/queries#inserts

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM