这个问题类似于这个question
我没有更改表迁移中的默认 autoIncrement
,但是我注意到我无法从特定行的 ID
获取模型。对于其他行,它工作正常。我清除了缓存,认为这是缓存问题,但似乎不是。
行为
我得到了记录 1,..., 58, 59, 60
当我选择模型时
$object = Post::find(59);
// $object returns null but record exists
但是我通过应用程序添加了另一条记录以检查行为是否相同,60
中的记录不为空,这是预期的行为。有没有人遇到过这个?如果是这样,克服这个问题的最佳方法是什么。
我在 Windows 上使用 XAMPP v8.0.8
编辑: 发布模型
class Post extends Model
{
use HasFactory,Searchable,SoftDeletes;
protected $fillable = [
'hash_id','user_id','location','subjects','request_type'
];
protected $casts = [
'location' => 'array',
'subjects' => 'array'
];
public function User()
{
return $this->belongsTo('App\Models\User');
}
public function searchableAs()
{
return 'posts';
}
}
迁移文件
public function up()
{
Schema::create('posts', function (Blueprint $table) {
$table->id();
$table->string('hash_id')->unique();
$table->integer('user_id');
$table->json('location');
$table->json('subjects');
$table->string('request_type');
$table->timestamps();
$table->softDeletes();
});
}
最佳答案
假设从软删除中删除记录会发生这种情况。尝试查看数据库中的 deleted_at 字段。
关于mysql - Eloquent 查询不选择特定的 ID,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/69157627/