[英]Laravel 5.7 return id null after save()
When I have started with the project using Laravel 5.7 and PHP 7.3 , $info->id
returned correctly the last insert id.当我使用Laravel 5.7和PHP 7.3开始项目时,
$info->id
正确返回了最后一个插入 ID。 Now, I did some changes at the login file ( all changes are here ) and not at this files, but it doesn't work.现在,我对登录文件进行了一些更改( 所有更改都在这里)而不是在此文件中,但它不起作用。
The gf_id in the table is autoincrement , the function $info->id return null .表中的gf_id是autoincrement ,函数$info->id 返回 null 。
I have tried to delete the row " public $incrementing = false;
" and to set it to false but there aren't any changes.我试图删除行“
public $incrementing = false;
”并将其设置为 false 但没有任何更改。 I have tried with other function like $info->_id
or $this->id
and also have changed $info->save
with $info->create
but nothing, always null.我曾尝试使用其他函数,如
$info->_id
或$this->id
并且还使用$info->create
更改了$info->save
但什么都没有,始终为 null。
If I use getPrimaryKey return the error " Call to undefined method ".如果我使用getPrimaryKey返回错误“调用未定义的方法”。
In the database the row is created correctly.在数据库中,该行已正确创建。
If I print the object $info with dd($info);
如果我用
dd($info);
打印对象 $info dd($info);
, the result is this: ,结果是这样的:
GfTableModel {#233
#table: "gf_table"
#primaryKey: "gf_id"
+timestamps: false
+incrementing: false
#connection: "mysql"
#keyType: "int"
#with: []
#withCount: []
#perPage: 15
+exists: true
+wasRecentlyCreated: true
#attributes: array:5 [
"gf_user" => 5
"gf_new" => 1
"gf_data" => "2019-03-22 14:08:28"
"gf_object" => ""
"gf_text" => ""
]
#original: array:5 [
"gf_user" => 5
"gf_new" => 1
"gf_data" => "2019-03-22 14:08:28"
"gf_object" => ""
"gf_text" => ""
]
#changes: []
#casts: []
#dates: []
#dateFormat: null
#appends: []
#dispatchesEvents: []
#observables: []
#relations: []
#touches: []
#hidden: []
#visible: []
#fillable: []
#guarded: array:1 [
0 => "*"
]
}
This is my GfTableModel.php :这是我的GfTableModel.php :
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class GfTableModel extends Model
{
protected $table = 'gf_table';
protected $primaryKey = 'gf_id';
public $timestamps = false;
public $incrementing = true;
public function insertNewRow(){
$info = new GfTableModel();
$info->gf_user = auth()->id();
$info->gf_data = date("Y-m-d H:i:s");
$info->gf_new = 1;
$info->gf_object = "";
$info->gf_text = "";
$info->save();
return $info->id;
}
}
Where am I doing wrong?我哪里做错了? Could it be a problem given by the command
php artisan config: cache
?这可能是命令
php artisan config: cache
给出的问题吗? Thank you谢谢
I ran into something similar.我遇到了类似的事情。 As of Laravel 5.7, 5.8 you can do this:
从 Laravel 5.7、5.8 开始,你可以这样做:
return $info->getKey();
This works regardless of the name of the primary key.无论主键的名称如何,这都有效。
@ryantxr解决方案:
return $info->gf_id
Replace this:替换这个:
$info = new GfTableModel();
With:和:
$info = GfTableModel::create();
I also face this problem.我也面临这个问题。 Solution is :
解决方案是:
return $info->getKey();
This method is useful when you don't know the name of primary key field.当您不知道主键字段的名称时,此方法很有用。 This method get the value of the model's primary key.
此方法获取模型主键的值。 It will returns a integer primary key value which is inserted using
$info->save();
它将返回一个使用
$info->save();
插入的整数主键值$info->save();
method.方法。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.