راکت
حل شده

updateOrCreate

13 پاسخ

سلام دوستان من این کدو نوشتم ارور میده باید چطوری بنویسم که ارور نده

 $shoppingCart = $this->shoppingCart->where('product_id', $product->id)->first();
        $shoppingCart->updateOrCreate([
            'product_id' => $product->id,
            'user_id' => $this->user_id,
            'purchase_code' => \Str::random(10),
            'price' => $this->price,
            'total_price' => $this->price,
        ], [
            'quantity' => $shoppingCart->quantity + $quantity,
            'total_price' => $this->price * ($shoppingCart->quantity + $quantity)
        ]);

ارور این میده

Call to a member function updateOrCreate() on null 
ثبت پرسش جدید

پاسخ‌ها

13
Saman
Saman
@samanzdev

تخصص: برنامه نویس وب

بهترین پاسخ

سلام
مطمئنید که اطلاعاتی از سمت request میاد null نیستن؟
یه dd از تک تک request ها بگیر ببين خالی نیستن؟

hussein
hussein
@husseinhashim1133

تخصص: طراحی وب

سلام من از بیرون فقط ایدی محصول میگیرم و تعداد محصول این کدی که نوشتم میخوام خلاصه ترش کنم
این کد اجرا میشه کاملا

  if ($shoppingCart = ShoppingCart::where('product_id', $product->id)->first()) {
            if (($shoppingCart->quantity + $quantity) <= $product->inventory) {
                $shoppingCart->update([
                    'quantity' => $shoppingCart->quantity + $quantity,
                    'total_price' => $this->getPrice($product) * ($shoppingCart->quantity + $quantity)
                ]);
            }
        } else {
            ShoppingCart::create([
                'product_id' => $product->id,
                'user_id' => $this->user_id,
                'purchase_code' => \Str::random(10),
                'quantity' => $quantity,
                'price' => $this->getPrice($product),
                'total_price' => $this->getPrice($product) * ($quantity)
            ]);
        }

@samanzdev

Saman
Saman
@samanzdev

تخصص: برنامه نویس وب

این کدتون خیلی اشتباس، می تونید از if خلاصه شده استفاده کنید.
https://davidwalsh.name/php-ternary-examples

شاید پاسخ شما همین‌جا باشد

hussein
hussein
@husseinhashim1133

تخصص: طراحی وب

خب میگم میخوام خلاصش کنم تروتمیز

hussein
hussein
@husseinhashim1133

تخصص: طراحی وب

این روش الان درسته ؟

 $shoppingCart = ShoppingCart::where('product_id', $product->id)->first();
        return $shoppingCart && ($shoppingCart->quantity + $quantity) <= $product->inventory ?
            $shoppingCart->update([
                'quantity' => $shoppingCart->quantity + $quantity,
                'total_price' => $this->getPrice($product) * ($shoppingCart->quantity + $quantity)
            ]) :
            ShoppingCart::create([
                'product_id' => $product->id,
                'user_id' => $this->user_id,
                'purchase_code' => \Str::random(10),
                'quantity' => $quantity,
                'price' => $this->getPrice($product),
                'total_price' => $this->getPrice($product) * ($quantity)
            ]);

@samanzdev

Saman
Saman
@samanzdev

تخصص: برنامه نویس وب

آره درسته ولی سعی کن خلاصه ترش کنی.

hussein
hussein
@husseinhashim1133

تخصص: طراحی وب

مشکل اینجاست updateOrCreate کار نمیکنه پیشم
به هرنحوی زدم نشد داکیونتشو خوندم همون روش رفتم باز نشد

@samanzdev

Saman
Saman
@samanzdev

تخصص: برنامه نویس وب

نمی دونم به چه شکل می نویسی که کار نمیکنه 🤔
اما به این روش که نوشتی درسته اما خلاصه ترش کن.

hussein
hussein
@husseinhashim1133

تخصص: طراحی وب

 $shoppingCart = $this->shoppingCart->where('product_id', $product->id)->first();
        $shoppingCart->updateOrCreate([
            'product_id' => $product->id,
            'user_id' => $this->user_id,
            'purchase_code' => \Str::random(10),
            'price' => $this->price,
            'total_price' => $this->price,
        ], [
            'quantity' => $shoppingCart->quantity + $quantity,
            'total_price' => $this->price * ($shoppingCart->quantity + $quantity)
        ]);

به این روش نوشتم
@samanzdev

Saman
Saman
@samanzdev

تخصص: برنامه نویس وب

خب بالاتر گفتم که چک کن شاید request هات خالی باشن.

hussein
hussein
@husseinhashim1133

تخصص: طراحی وب

خالی نیست فقط پروداکت ایدی میگیرم
بقیه خودم تعریف کردم اگر خالی باشه کدی که من نوشتم بالا نشون دادم کار نمیکرد درصورتیکه کار میکنه
@samanzdev

Saman
Saman
@samanzdev

تخصص: برنامه نویس وب

آیدی آنی دسک رو بگو

hussein
hussein
@husseinhashim1133

تخصص: طراحی وب

برای ارسال پاسخ لازم است وارد شده یا ثبت‌نام کنید

ورود یا ثبت‌نام