hussein
2 سال پیش توسط hussein مطرح شد
13 پاسخ

updateOrCreate

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

 $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 

ثبت پرسش جدید
Saman
تخصص : برنامه نویس وب
@samanzdev 2 سال پیش مطرح شد
0

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


hussein
تخصص : طراحی وب
@husseinhashim1133 2 سال پیش آپدیت شد
0

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

  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
تخصص : برنامه نویس وب
@samanzdev 2 سال پیش مطرح شد
0

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


hussein
تخصص : طراحی وب
@husseinhashim1133 2 سال پیش مطرح شد
0

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


hussein
تخصص : طراحی وب
@husseinhashim1133 2 سال پیش مطرح شد
0

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

 $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
تخصص : برنامه نویس وب
@samanzdev 2 سال پیش مطرح شد
0

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


hussein
تخصص : طراحی وب
@husseinhashim1133 2 سال پیش آپدیت شد
0

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

@samanzdev


Saman
تخصص : برنامه نویس وب
@samanzdev 2 سال پیش مطرح شد
0

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


hussein
تخصص : طراحی وب
@husseinhashim1133 2 سال پیش مطرح شد
0
 $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
تخصص : برنامه نویس وب
@samanzdev 2 سال پیش مطرح شد
0

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


hussein
تخصص : طراحی وب
@husseinhashim1133 2 سال پیش مطرح شد
0

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


Saman
تخصص : برنامه نویس وب
@samanzdev 2 سال پیش مطرح شد
0

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


hussein
تخصص : طراحی وب
@husseinhashim1133 2 سال پیش آپدیت شد
0

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

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