【发布时间】:2019-08-13 14:13:48
【问题描述】:
当我开始使用 Laravel 5.7 和 PHP 7.3 的项目时,$info->id 正确返回了最后一个插入 ID。现在,我在登录文件 (all changes are here) 上做了一些更改,而不是在这个文件上,但它不起作用。
表中的gf_id是autoincrement,函数$info->id return null。
我试图删除“public $incrementing = false;”行并将其设置为 false,但没有任何更改。我已经尝试过使用其他功能,如$info->_id 或$this->id,并且也将$info->save 更改为$info->create,但没有,始终为空。
如果我使用 getPrimaryKey,则返回错误“Call to undefined method”。
在数据库中,该行已正确创建。
如果我用dd($info); 打印对象$info,结果是这样的:
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 => "*"
]
}
这是我的 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;
}
}
我哪里做错了?会不会是命令php artisan config: cache给出的问题?
谢谢
【问题讨论】:
-
也许可以试试
return $info->gf_id -
就像我写的那样,我已经尝试将其设置为 true “我试图删除行 'public $incrementing = false;'并将其设置为 true 但没有任何更改”。
-
@ryantxr 是的!我不知道为什么,但它有效。我以前也试过这个,但它没有用,但现在是的,太完美了,也许我写错了。非常感谢!