Hyperf 插入json数据需要注意

我本来的代码是使用了firstOrCreate ,但是实际create才会调用到

模型文件

protected array $casts = ['id' => 'integer', 'created_at' => 'datetime', 'updated_at' => 'datetime', 'shop_id' => 'integer', 'question_id' => 'integer', 'book_category_id' => 'integer', 'user_id' => 'integer', 'book_id' => 'integer','images'=>'array'];

使用地方

UserQuestionFeedback::create([
    'book_id' => $book_id,
    'book_category_id' => $bookInfo['categories_id'],
    'user_id' => $user['id'],
    'question_id' => $question_id,
    'phone' => $phone,
    'content' => $content,
    'images' => $images,
    'shop_id' => $this->shop_id
]);

 

create跟firstOrcreate源码

public function create(array $attributes = [])
    {
        return tap($this->newModelInstance($attributes), function ($instance) {
            $instance->save();
        });
    }

public function firstOrCreate(array $attributes, array $values = [])
    {
        if (! is_null($instance = $this->where($attributes)->first())) {
            return $instance;
        }

        return tap($this->newModelInstance($attributes + $values), function ($instance) {
            $instance->save();
        });
    }

这个问题就很明显了

posted @ 2024-07-15 13:16  CFFIRM  阅读(50)  评论(0)    收藏  举报