2018-01-18 15:47
浏览 244

如何在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,所有工作都很好,直到我尝试使用输出 我的插入查询中的子句。</ p>

查询类似于</ p>

  INSERT INTO TABLE(列)OUTPUT INSERTED.MyDesiredReturnColumn VALUES(Value)\  n </ code> </ pre> 

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

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

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

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

这是打印“inserted id is”</ p>

这是我的模型:</ p>

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']; 
 </ code> </ pre> 

任何想法 会非常有帮助的。</ p> </ div>

3条回答 默认 最新

相关推荐 更多相似问题