函数static :: created无法正确保存Laravel [英] function static::created does not save properly Laravel

查看:58
本文介绍了函数static :: created无法正确保存Laravel的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我有2个表格:项目和产品.一个商品有很多产品,而一个商品属于一个商品.

So I have 2 tables: Item and Product. An Item hasMany Products and a Product belongsTo an Item.

产品迁移:

Schema::create('products', function (Blueprint $table) {
    $table->bigIncrements('id');
    $table->string('hashed_id')->nullable()->unique();
    $table->bigInteger('item_id')->unsigned();
    $table->bigInteger('user_id')->unsigned();
    $table->integer('state')->default(1);
    $table->decimal('price');
    $table->string('slug')->nullable()->unique();
    $table->timestamps();

    $table->foreign('item_id')->references('id')->on('items')->onDelete('cascade');
    $table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');
});

对于hashed_id,我使用以下程序包: https://packagist.org/packages/hashids/hashids ,以创建一个显示在网址中的哈希ID.

For the hashed_id I use the following package: https://packagist.org/packages/hashids/hashids to create a hashed id to show in the url.

Product.php

Product.php

public static function boot()
{
    parent::boot();

    static::created(function ($product) {
        $productId = $product->id;
        $hashids = new Hashids("", 10, 'abcdefghijklmnopqrstuvwxyz1234567890');
        $hashedId = $hashids->encode($productId++);

        $slug = Str::slug($product->item->slug . '-' . $hashedId);

        $product->hashed_id = $hashedId;
        $product->slug = $slug;
    });

}

ProductsController.php

ProductsController.php

public function createSelfProduct(Request $request)
{
    $product = auth()->user()->products()->create([
        'item_id' => $request->item_id,
        'user_id' => auth()->user()->id,
        'price' => $request->price,
    ]);

    // create the product and show seller info
    return new ProductResource($product->load('user'));
}

我想做的是,当用户创建新产品时,它应该从商品模型中获取 slug ,并在其后放置 $ hashedId 并将其保存到数据库.现在,当我通过邮递员发出发帖请求时,由于保存了 hashed_id slug ,我得到了所需的结果.但是当我检查数据库时, hashed_id slug 均为NULL.仅保存 item_id user_id price .我在做什么错了,我该如何解决?

What I'm trying to do is that when a user creates a new product, it should get the slug from the item model, put the $hashedId behind it and save that to the db. Now, when I do a post request via Postman, I get the desired result, as hashed_id and slug are saved. But when I check the database, both hashed_id and slug are NULL. Only the item_id, user_id and price are saved. What am I doing wrong and how can I fix this?

推荐答案

created 事件表示已经创建了模型.这不是保存之前,而是保存之后.如果您此时更改了任何内容,则需要再次保存记录.

The created event means the Model has already been created. This is not before save, but after it has been saved. If you alter anything at this point you will need to save the record again.

简单:您忘记在模型实例上调用 save 来保存更改.

Simply: You forgot to call save on your model instance to save the changes after you altered it.

这篇关于函数static :: created无法正确保存Laravel的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆