AndreLan

Experience

360

0 Best Reply Awards

  • Member Since 1 Week Ago
  • 0 Lessons Completed
  • 0 Favorites

11th October, 2018

AndreLan left a reply on How To Solve Problems Of Deleting From Orders Or Products • 4 days ago

<?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');

    }  
    
}

AndreLan left a reply on How To Solve Problems Of Deleting From Orders Or Products • 4 days ago

@jekinney this is my order controller.

AndreLan left a reply on How To Solve Problems Of Deleting From Orders Or Products • 4 days ago

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

AndreLan started a new conversation How To Solve Problems Of Deleting From Orders Or Products • 5 days ago

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>

10th October, 2018

AndreLan left a reply on Relation Issue Between Orders Quantity And Products Inventory • 6 days ago

Any help guys with this issue ???

9th October, 2018

AndreLan left a reply on Relation Issue Between Orders Quantity And Products Inventory • 1 week ago

@Vilfago

I have 4 tables in my database (user_type_id , users , products and orders, orders table contains 4 columns (id , product_id , user_id and qty), user_id and product_id = foreign keys of users and products tables.

orders table contains

public function user()
    {
        return $this->belongsTo('App\User');
    }
    public function product()
    {
        return $this->belongsTo('App\Product');
    }

AndreLan started a new conversation Relation Issue Between Orders Quantity And Products Inventory • 1 week ago

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 -1 -2 -3 etc..

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 :D 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>

8th October, 2018

AndreLan left a reply on How To Solve (Trying To Get Property Of Non-object) Error, But The Same Function Works Well In Another Function • 1 week ago

ok, thanks for all :) , I fixed it, It was the action of the view form :D action="/update_order/{{ $order['id'] }}" , It should be action="/update_order/{{ $order->product->id }}" ,,

Now :)

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 -1 -2 -3 etc..

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 told me how to use the commented spans because I do not know how to do that also :D thanks again..

AndreLan left a reply on Trying To Get Property Of Non-object But The Function Works Well In Another Function • 1 week ago

@tykus yes, I understood, but using findOrFail($id) in place of find($id) for which function .. or for all of them?

AndreLan left a reply on Trying To Get Property Of Non-object But The Function Works Well In Another Function • 1 week ago

@tykus

I do not understand where to use this or for doing what ??

AndreLan started a new conversation Trying To Get Property Of Non-object But The Function Works Well In Another Function • 1 week ago

what is wrong with this controller, I always get this error when update order "Trying to get property of non-object" ,(notice that I'm using the same function for store and it works fine, also 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 and I found that it keeps deleting till -1 -2 -3 etc.. 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 told me how to use the commented spans because I do not know how to do that also :D thanks

    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;
    
    class OrderController extends Controller
    {
    
        public function __construct()
        {
            $this->middleware('auth');
        }
    
        public function myorders()
        {
            $orders = Order::where('user_id','=',Auth::user()->id)->orderBy('id','desc')->get();
            return view('order.myorders',compact('products','orders','users'));
        }   
    
    
    
    
    
    
      public function buy(Request $id)
        {
            $product = Product::find($id);
            return view('product.index',compact('product','order','user'));
        }
    
    public function store(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();
            $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);
            return view('order.myorders',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();
            $order->user_id = Auth::user()->id;
            $order->product_id = $id;
            $order->qty = $request['qty'];                                  
  this line $product->inv = $product->inv - $order->qty; (it works in store)
            $order->save();
            $product->save();
    
            return redirect('/myorders');
        
        }
    
    
            public function delete($id)
        {
            $order = Order::find($id);
            return view('order.myorders',compact('product','order','user'));
        }
    
    
              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 routes file

Route::get('/myorders', '[email protected]');
Route::get('/add_order', '[email protected]');
Route::get('/add_order/{id}', '[email protected]');
Route::get('/update_order', '[email protected]');
Route::get('/update_order/{id}', '[email protected]');
Route::get('/delete_order', '[email protected]');
Route::get('/delete_order/{id}', '[email protected]');

and this is my view, I am using it with

<div class="klaviyo_modal" id="update_order_qty" style="display:none;">
    <div class="klaviyo_inner">
        <a href="Cancel" class="klaviyo_close_modal klaviyo_header_close">×</a>
            <form method="GET" action="/update_order/{{ $order['id'] }}" enctype="multipart/form-data">
                {{ csrf_field() }}
                    <p class="klaviyo_header">Please update quantity as you need, then order it.</p>
                        <div class="klaviyo_fieldset">
                            <div class="{{ $errors->has('qty') ? ' has-error' : '' }}">
                                <label for="qty" style="display:block;text-align:center;margin-bottom:25px">Ordered New Quantity will be ADDED to your CURRENT Order Quantity</label>
                                    <input type="number" id="qty" class="qty" name="qty" style="display:block;margin:auto" required="required" placeholder="CURRENT Quantity">
                                    @if ($errors->has('qty'))
                                    <span class="help-block">
                                         <strong style="display:block;text-align:center">{{ $errors->first('qty') }}</strong>
                                    </span>
                                    @endif
                            </div>
                        </div>
          <div class="klaviyo_fine_print"></div>
              <div class="klaviyo_form_actions">
                 <button type="submit" class="klaviyo_submit_button">
                        <span>Update Order Quantity Now</span>
                 </button>
              </div>



            <!-- <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> -->        



            </form>
    </div>
</div>

6th October, 2018

AndreLan left a reply on When I Press The Buy Button I Get This Error • 1 week ago

@taylormaguire Do you mean this ??

Route::get('/add_order/{id}', '[email protected]');

AndreLan left a reply on When I Press The Buy Button I Get This Error • 1 week ago

It gave me this Request {#40 ▼ #json: null #convertedFiles: null #userResolver: Closure {#124 ▶} #routeResolver: Closure {#148 ▶} +attributes: ParameterBag {#42 ▶} +request: ParameterBag {#48 ▶} +query: ParameterBag {#48 ▶} +server: ServerBag {#45 ▶} +files: FileBag {#44 ▶} +cookies: ParameterBag {#43 ▶} +headers: HeaderBag {#46 ▶} #content: null #languages: null #charsets: null #encodings: null #acceptableContentTypes: null #pathInfo: "/add_order/2" #requestUri: "/add_order/2" #baseUrl: "" #basePath: null #method: "GET" #format: null #session: Store {#166 ▶} #locale: null #defaultLocale: "en" }

AndreLan left a reply on When I Press The Buy Button I Get This Error • 1 week ago

@taylormaguire

the same error

QueryException in Connection.php line 729: SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'qty' cannot be null (SQL: insert into orders (user_id, product_id, qty, updated_at, created_at) values (2, 2, , 2018-10-07 01:20:19, 2018-10-07 01:20:19))

AndreLan left a reply on When I Press The Buy Button I Get This Error • 1 week ago

@mattsplat I got this :D

Request {#40 ▼ #json: null #convertedFiles: null #userResolver: Closure {#124 ▶} #routeResolver: Closure {#148 ▶} +attributes: ParameterBag {#42 ▶} +request: ParameterBag {#48 ▶} +query: ParameterBag {#48 ▶} +server: ServerBag {#45 ▶} +files: FileBag {#44 ▶} +cookies: ParameterBag {#43 ▶} +headers: HeaderBag {#46 ▶} #content: null #languages: null #charsets: null #encodings: null #acceptableContentTypes: null #pathInfo: "/add_order/2" #requestUri: "/add_order/2" #baseUrl: "" #basePath: null #method: "GET" #format: null #session: Store {#166 ▶} #locale: null #defaultLocale: "en" }

AndreLan left a reply on When I Press The Buy Button I Get This Error • 1 week ago

@taylormaguire

No, it is just for the js , and to get sure I did what you said and changed it to qty and the same error

AndreLan left a reply on When I Press The Buy Button I Get This Error • 1 week ago

I changed again to avoid misunderstanding :D

AndreLan left a reply on When I Press The Buy Button I Get This Error • 1 week ago

No. It is called qty, I just changed it in the error message to define that it is an integer

AndreLan started a new conversation When I Press The Buy Button I Get This Error • 1 week ago

When I press the buy button I get this error

QueryException in Connection.php line 729: SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'quantity' cannot be null (SQL: insert into orders (user_id, product_id, quantity, updated_at, created_at) values (3, 1, , 2018-10-06 20:44:27, 2018-10-06 20:44:27))

first of all, I'm a beginner so I'm still learning and I do not know if some of this is correct, all I want is when I press the buy button it will display the form and I will type the quantity then submit and my quantity will increase and the products inventory will decrease and if I deleted my order, The products will increase again (I did this and I think it will work) and the user can update his quantity and if someone ordered something the seller cannot delete it, only update his inventory as same as the buy button way and the form, and if the users ordered all products it will display (out of stock) with a dead link button, I am training, It is not homework and I do not know how to write all functions..

this is the Buy button

<li><span><a href="/add_order/{{ $product['id'] }}">Buy</a></span></li>

which should pop up the next form when I press it,

this is my form code for the buy button

<form method="POST" action="/add_order/{{ $product['id'] }}" class="klaviyo_subscription_form" enctype="multipart/form-data">
                {{ csrf_field() }}
                    <input type="hidden" name="g" value="LIST_ID">
                        <div class="klaviyo_fieldset">
                            <p class="klaviyo_header">Please define quantity you need, then order it.</p>
                        </div>
                            <div class="klaviyo_fieldset">
                                <div class="klaviyo_field_group{{ $errors->has('qty') ? ' has-error' : '' }}">
                                    <label for="k_id_modal_$email" style="display: block; text-align:center">Quantity</label>
                                        <input type="number" id="k_id_modal_$email" name="qty" style="display: block; margin:auto">
                                        @if ($errors->has('qty'))
                                        <span class="help-block">
                                            <strong>{{ $errors->first('qty') }}</strong>
                                        </span>
                                        @endif
                                </div>
                            </div>

                                <div class="klaviyo_fine_print"></div>
                                    <div class="klaviyo_form_actions">
                                        <button type="submit" class="klaviyo_submit_button">
                                            <span>Order Now</span>
                                        </button>
                                    </div>
                                <div class="klaviyo_below_submit" ></div>
            </form>

and this is the OrderController functions

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;

class OrderController extends Controller
{

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

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

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

public function destroy($id)
    {
        $order = Order::find($id);
        $product= Product::find($order->product_id);
        $product->inv = $product->inv + 1;
        $product->save();
        $order->delete();
        return redirect('/myorders');

    }

thanks for reading :D

Edit Your Profile
Update

Want to change your profile photo? We pull from gravatar.com.