2018-01-18 15:47
浏览 209

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

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.

Query is something like


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)

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.

UPDATES: The field I am attempting to retrieve is a uniqueidentifier field (guid) that is created in SQL, not from Laravel-side

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.

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

This is printing "inserted id is "

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.

图片转代码服务由CSDN问答提供 功能建议

我正在使用Laravel和sqlsrv,连接到SQL Server 2016,所有工作都很好,直到我尝试使用输出 我的插入查询中的子句。


  INSERT INTO TABLE(列)OUTPUT INSERTED.MyDesiredReturnColumn VALUES(Value)\  n   

这在SQL Server中完美运行,并返回所需的值,但使用Laravel的DB :: insert功能,它只返回1(成功插入)< / p>

我有一个我现在不想拥有的解决方法,使用CreatedOn字段返回最近创建的行,但这有潜在的问题。 < p>更新:我试图检索的字段是一个在SQL中创建的uniqueidentifier字段(guid),而不是来自Laravel端

尝试@ PrathameshPalav建议使用Eloquent模型 创建时,值正在正确插入到DB中,但它不会返回un 仍然是iqueidentifier。

  $ inserted = MyModel :: create($ information); 
print“inserted id is”。  $ inserted-&gt; MyModelId; 

这是打印“inserted id is”


use Illuminate \ Database \ Eloquent \ Model;  
class MyModel扩展Model 
 protected $ table ='MyModelBase'; 
 protected $ primaryKey ='MyModelId'; 
 public $ incrementing = false; 
 protected $ keyType =“string”;  
 public $ timestamps = false; 
 protected $ fillable = ['field1','field2','etc']; 

任何想法 会非常有帮助的。

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

3条回答 默认 最新

  • doudu22272099831 2018-01-18 18:07

    The way that I solved this was by incorporating an Eloquent model (as pointed out by @PrathameshPalav), then (loosely) following this tutorial

    Specifically this part

    public static function 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 =)

    Thank you both for your help!

    点赞 评论
  • dpmopn8542 2018-01-18 16:03

    You can use Eloquent ORM for this purpose:

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

    It will return inserted model object in response. Or if you want only inserted record id then use

    点赞 评论
  • duanci8209 2018-01-18 16:06

    Yes, when you use insert it will return a bool. You can use insertGetId to get the the id.

    If the table has an auto-incrementing id, use the insertGetId method to insert a record and then retrieve the ID:

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

    More info:

    点赞 评论

相关推荐 更多相似问题