首页 > 解决方案 > 调用启动事件 laravel

问题描述

会员

我一直在做一个项目,遇到一种情况,当在第一个表(产品变量表)中插入新行时,我想在第二个表(优惠表)中创建一行。为此,我想到了使用 laravel 创建的引导事件,但我认为该事件无法正常工作。

这是我的代码:

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

        static::creating(function($variant) {
            $variant->product->categories()->each(function($category){
                $offers = \App\OfferReference::where([['reference_id',$category->id],['reference','Category']])
                ->orWhere([['reference_id',$category->id],['reference','Subcategory']])->get();
                foreach ($offers as $offer) {
                    $table = \App\Offer::where([['product_variant_unique_stock_id',$variant->unique_stock_id],['offer_reference_id',$offer->id]])->first();
                    if(!$table)
                    {
                        $table = new \App\Offer;
                        $table->product_variant_unique_stock_id = $variant->unique_stock_id;
                        $table->offer_reference_id = $offer->id;
                        $table->save();
                    }
                }
            });
            $offers = \App\OfferReference::where([['reference_id',$variant->product->id],['reference','Product']])->get();
            foreach ($offers as $offer) {
                $table = \App\Offer::where([['product_variant_unique_stock_id',$variant->unique_stock_id],['offer_reference_id',$offer->id]])->first();
                if(!$table)
                {
                    $table = new \App\Offer;
                    $table->product_variant_unique_stock_id = $variant->unique_stock_id;
                    $table->offer_reference_id = $offer->id;
                    $table->save();
                }
            }
            return true;
        });
    }

优惠表中没有创建任何内容。

谁能指导我我做错了什么或任何解决方案或想法来解决我的问题

标签: laraveleventseloquentmodelboot

解决方案


好吧,我终于解决了!

实际上代码很好,只是我没有在categories->each() 函数中传递$variant

工作代码是

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

        static::creating(function($variant) {
            $variant->product->categories()->each(function($category) use ($variant) {
                $offers = \App\OfferReference::where([['reference_id',$category->id],['reference','Category']])
                ->orWhere([['reference_id',$category->id],['reference','Subcategory']])->get();
                foreach ($offers as $offer) {
                    $table = \App\Offer::where([['product_variant_unique_stock_id',$variant->unique_stock_id],['offer_reference_id',$offer->id]])->first();
                    if(!$table)
                    {
                        $table = new \App\Offer;
                        $table->product_variant_unique_stock_id = $variant->unique_stock_id;
                        $table->offer_reference_id = $offer->id;
                        $table->save();
                    }
                }
            });
            $offers = \App\OfferReference::where([['reference_id',$variant->product->id],['reference','Product']])->get();
            foreach ($offers as $offer) {
                $table = \App\Offer::where([['product_variant_unique_stock_id',$variant->unique_stock_id],['offer_reference_id',$offer->id]])->first();
                if(!$table)
                {
                    $table = new \App\Offer;
                    $table->product_variant_unique_stock_id = $variant->unique_stock_id;
                    $table->offer_reference_id = $offer->id;
                    $table->save();
                }
            }
            return true;
        });
    }

第4 行使用 ($variant)是阻止我的代码工作的原因


推荐阅读