【问题标题】:Eloquent query doesn't select a specific IDEloquent 查询不会选择特定的 ID
【发布时间】:2021-09-13 05:22:34
【问题描述】:

这个问题和这个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();
        });
    }

【问题讨论】:

  • 你能用你的 Post 模型和数据库表结构更新你的问题吗?
  • @toyi,刚刚更新

标签: mysql laravel eloquent


【解决方案1】:

假设从软删除中删除记录时会发生这种情况。尝试查看数据库中的 deleted_at 字段。

【讨论】:

  • 哦,我的.. 我已经删除了记录,但错过了检查deleted_at。谢谢你的收获。
猜你喜欢
  • 2017-11-13
  • 1970-01-01
  • 2011-06-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-02-22
相关资源
最近更新 更多