Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/spring-boot/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php Laravel模型创建不返回MS SQL上的主键_Php_Sql Server_Laravel_Eloquent - Fatal编程技术网

Php Laravel模型创建不返回MS SQL上的主键

Php Laravel模型创建不返回MS SQL上的主键,php,sql-server,laravel,eloquent,Php,Sql Server,Laravel,Eloquent,我有一个模型帐户,它使用第三方MS SQL数据库。 我可以创建、更新和删除帐户,但在Account::create(['logiuser'=>'jdoe','loginpwd'=>'supersecret'])之后,属性“rowguid”为空。 rowguid是唯一标识符列 Account::find('28B7F554-9689-4DFD-9C29-1CDAC6513436')。 但我需要创建后的rowguid,将其作为属性存储在另一个模型中 所以我尝试手动“创建” $conn=DB::co

我有一个模型帐户,它使用第三方MS SQL数据库。 我可以创建、更新和删除帐户,但在
Account::create(['logiuser'=>'jdoe','loginpwd'=>'supersecret'])之后,
属性“rowguid”为空。 rowguid是唯一标识符列

Account::find('28B7F554-9689-4DFD-9C29-1CDAC6513436')
。 但我需要创建后的rowguid,将其作为属性存储在另一个模型中

所以我尝试手动“创建”


$conn=DB::connection('sqlsrv')->getPdo();
$sqlString=“DECLARE@lastinsertid表(rowguid uniqueidentifier);插入dbo.accounts(logiuser,loginpwd)输出INSERTED.rowguid到@lastinsertid值(?),从@lastinsertid;中选择rowguid;”;
$sqlVals=['jdoe','supersecret'];
$stmt=$conn->prepare($sqlString);
$stmt->execute($sqlVals);
$temp=$stmt->fetch(PDO::fetch_ASSOC);
这导致:

从字符串转换为uniqueidentifier时出现异常,消息为“SQLSTATE[42000]:[Microsoft][ODBC Driver 17 for SQL Server][SQL Server]转换失败。”

在SQL Studio上运行此命令可以工作并返回rowguid:

DECLARE@lastinsertid表(rowguid uniqueidentifier);
INSERT INTO accounts(loginuser,loginpwd)输出INSERTED.rowguid到@lastinsertid值('johndoe','supersecret');
从@lastinsertid中选择rowguid;

我正在使用Laravel 8.32.1和linux上的PHP8.0.3以及msodbcsql17-17.7.2.1-1,将其移动到一个正式的答案,因为上的链接可能会发生变化,答案是通过注释找到的

Tippin展示了如何使用以下特性,因为Laravel将Ramsey包合并到了
Str::class
facade中。这是为了解决UUID+SQL Server的问题

<?php

namespace App\Traits;

use Illuminate\Database\Eloquent\Model;
use Str;

trait Uuids
{
    /**
     * On model creating, set the primary key to UUID
     */
    public static function bootUuids()
    {
        static::creating(function (Model $model) {
            $model->{$model->getKeyName()} = Str::orderedUuid()->toString();
        });
    }
}

使用此处所示的特征是否有效?使用lastInsertId()方法$lastId=DB::getPdo()->lastInsertId()尝试一下@hppycoder thx,该特性确实有效@帕特里克·赫普勒听到这个消息真高兴!我将此作为一个正式的回答,以帮助遇到StackOverflow的其他人寻找相同的东西。我不确定Tippin是否在这里(我想他们在),但我感谢他们所做的工作。
$test = User::create([
    'first' => 'Derpy',
    'last' => 'Herpy',
    'slug' => '12345',
    'active' => 1,
    'email' => 'ok@ok.com',
    'password' => 'password'
]);

dump($test->id);

//"91e85023-4508-4c90-972b-5298e1768702"

$test2 = new User();
$test2->first = 'nope';
$test2->last = 'never';
$test2->slug = '8888';
$test2->active = 1;
$test2->email = 'whyr@lolol.com';
$test2->password = '987987987';
$test2->save();

dump($test2->id);

//"91e85023-55a7-46e0-801c-8fa9ed9a846a"