how to solve problems of deleting from orders or products

Published 1 week ago by AndreLan

I want something I do not know how to write it correctly, I want when a customer delete an order and it is only one order (quantity = 1), to be deleted permanently and also if he typed the same quantity, to be deleted permanently also because when I tested this function I found that it keeps deleting till 2, 1, 0, -1, -2, -3, etc.. and increases the product quantity

and I want if the product quantity is 0 to hide the product for all users except the admins and the seller who sold it or at least to hide the Add to Cart button for all or replace it with dead button with out of stock instead of Add to Cart and if a user already ordered some of this product he cannot update quantity but can cancel it then it will be added to products again..

and I hope if someone tells me how to use these spans when errors happened or not because I do not know how to do that also or how to edit the default laravel error messages thanks, again...

this is the OrderController destroy function

public function destroy(Request $request, $id)
            {
            $validator = Validator::make($request->all(), [
                'qty' => 'required|max:255',

            ]);

            if ($validator->fails()) {
                return redirect('')
                            ->withErrors($validator)
                            ->withInput();
            }
            $order = Order::find($id);
        $product= Product::find($order->product_id);
        $order->qty = $order->qty - $request->input('qty');
        $product->inv = $product->inv + $order->qty;
        $product->save();
        $order->save();
            return redirect('/myorders');

        }  

and this is the span messages I want to run

<span class="help-block">
                  <div class="success_message" style="display: block; text-align:center">Order Updated Successfully</div><br>
            </span>   
            <span class="help-block">  
                  <strong class="error_message" style="display: block; text-align:center">Sorry, Product Out of Stock, Try again later</strong>
            </span>
jekinney
jekinney
1 week ago (215,485 XP)

use

 'qty' => 'required|between:0,255',
// or

 'qty' => 'required|min:0|max:255',

Your not performing really any checks on your queried data, that you still will need to do to be sure of the quantity available etc. But the validation of negative numbers is above.

AndreLan

@jekinney First of all, thank you for your reply.. I tried both of them and the same issue exactly..

AndreLan

@jekinney this is my order controller.

AndreLan
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Http\Requests;
use App\Order;
use App\Product;
use Auth;
use App\User;
use App\Details;
use Validator;
use App\Cart;
use Session;

class OrderController extends Controller
{

    public function __construct()
    {
        $this->middleware('auth');
    }

    public function my_cart()
    {
        $orders = Order::where('user_id','=',Auth::user()->id)->orderBy('id','desc')->get();
        return view('order.my_cart',compact('products','orders','users'));
    }   



  public function add_2_my_cart(Request $id)
    {
        $product = Product::find($id);
        // $product = Product::where('inv','=',$id)->get();
        return view('views.index',compact('product','order','user'));
    }


public function add_to_my_cart(Request $request, $id)
    

        {
        $validator = Validator::make($request->all(), [
            'qty' => 'required|max:255',
            
        ]);

        if ($validator->fails()) {
            return redirect('')
                        ->withErrors($validator)
                        ->withInput();
        }

        $product = Product::find($id);
        $order = new Order();

        $oldCart = Session::has('cart') ? Session::get('cart') : null;
        $cart = new Cart($oldCart);
        $cart->add($product, $product->id);
        $request->session()->put('cart',$cart);

        $order->user_id = Auth::user()->id;
        $order->product_id = $id;
        $order->qty = $request['qty'];
        $product->inv = $product->inv - $order->qty;
        $order->save();
        $product->save();
        return redirect('/');
    }
 
    
public function edit(Request $id)
    {
        $product = Product::find($id);
        // $product = Product::where('inv','=',$id)->get();
        return view('order.my_cart',compact('product','order','user'));
    }


public function update(Request $request, $id)
    

        {
        $validator = Validator::make($request->all(), [
            'qty' => 'required|max:255',
            
        ]);

        if ($validator->fails()) {
            return redirect('')
                        ->withErrors($validator)
                        ->withInput();
        }

        $product = Product::find($id);
        $order = new Order();

        $oldCart = Session::has('cart') ? Session::get('cart') : null;
        $cart = new Cart($oldCart);
        $cart->add($product, $product->id);
        $request->session()->put('cart',$cart);

        $order->user_id = Auth::user()->id;
        $order->product_id = $id;
        $order->qty = $request['qty'];
        $product->inv = $product->inv - $order->qty;
        $order->save();
        $product->save();
        return redirect('/my_cart');
    }


        public function delete($id)
    {
        $order = Order::find($id);
        // $product = Product::where('inv','=',$id)->get();
        return view('order.my_cart',compact('product','order','user'));
    }


          public function destroy(Request $request, $id)
        {
        $validator = Validator::make($request->all(), [
            'qty' => 'required|between:0,255',
            
        ]);

        if ($validator->fails()) {
            return redirect('')
                        ->withErrors($validator)
                        ->withInput();
        }
        $order = Order::find($id);
        $product= Product::find($order->product_id);
        $order->qty = $order->qty - $request['qty'];
        $product->inv = $product->inv + $request->input('qty');
        // if($order->qty == 1 || $order->qty){Order::destroy($id);}
        $product->save();
        $order->save();
        
        return redirect('/my_cart');

    }  
    
}

Please sign in or create an account to participate in this conversation.