0

i want to insert a record of form fields in table and also values from session array

i have a form with following fields

name,email,address

and i have session array 'cartS' like this

"cartS": [
  {
    "pid": "1",
    "qnty": "3"
  },
  {
    "pid": "2",
    "qnty": "2"
  }
],

so i have a table in which following fields

---------------------------------------------------------------------------------------------
id(auto increment)  uid(user's id)   pid(product id)   qnty(quantity)   name   email   address
---------------------------------------------------------------------------------------------

here i have model called order model

i have one controller in which there is public function placeorder(Request $request)

i can get value from form-fields using $request

but what about pid and qnty which are in session array(associative array)

if i do foreach loop then it only gives value of one key but i have 2 keys in session array

$sessionCart=Session::get('cartS');

foreach($sessionCart as $key=>$value){
    $pid=$value['pid'];
    $qnty=$value['qnty'];
}  

if i return $pid then it gives only first key's pid but not all

following is my placeorder function

public function placeOrder(Request $request){

        $sessionUser=Session::get('userS'); // user's name,email are stored in sessionarray called 'userS'
        $sessionEmail=$sessionUser['email']; // i am accessing email of session user 
  
        $getUid=customerModel::where('email',$sessionEmail)->count();
       // counting userid(uid) from table which should be only one unique in table

        if($getUid==1){
            $uid=customerModel::where('email',$sessionEmail)->value('uid');
            // here i am getting uid of that user
            
            // here i am requesting value of form field

            $name=$request->name;
            $email=$request->email;
            $address=$request->address;
            $order=new orderModel;
            
            $sessionCart=Session::get('cartS');
            if($sessionCart){

            // if $sessioncart is true then following code should run and rest is my garbage code.....

                foreach($sessionCart as $key=>$value){
                    $pid=$value['pid'];
                    $qnty=$value['qnty'];
                }   

                    $order->uid=$uid;
                    $order->pid=$pid;
                    $order->quantity=$qnty;
                    $order->customer_name=$name;
                    $order->customer_email=$email;
                    $order->address=$address;
                    $order->save();
                return redirect('checkout');
            }
            else{
                return back();
            }
        }
        else{
            return redirect('/');
        }
        
    }

i don't know if i am right or wrong by writing above function but i want to insert record like following

if it is not good approach then kindly suggest one : )

----------------------------------------------------------------------------------------------------
id(auto increment)  uid(user's id)   pid(product id)   qnty(quantity)   name   email         address
----------------------------------------------------------------------------------------------------
1                   1                1-2               3-2             xyz     [email protected]   xyz           

here i am trying to insert value of pid and qnty are using implode/explode

honestly i've never tried implode/explode

or

----------------------------------------------------------------------------------------------------
id(auto increment)  uid(user's id)   pid(product id)   qnty(quantity)   name   email         address
----------------------------------------------------------------------------------------------------
1                   1                1               3            xyz     [email protected]   xyz 
1                   1                2               2            xyz     [email protected]   xyz 

please give your suggestions-answers whatever etc : )

1 Answer 1

1

If there are 2 products in your cart, (i think) you must have 2 records in order table, also you need to add one more column to group orders, for example order_tag which will contain unique string Second way is to divide orders table in two tables

orders(id,user_id,name,email,address, order_status)
order_items(id,order_id,product_id,qnty,price)

Then you can easily find(query) orders detail. Saving products will look like that

if($sessionCart){

        // if $sessioncart is true then following code should run and rest is my garbage code.....
    $order=new orderModel;
    $order->uid=$uid;
    $order->customer_name=$name;
    $order->customer_email=$email;
    $order->address=$address;
    $order->save();
    foreach($sessionCart as $key=>$value){
       $pid=$value['pid'];
       $qnty=$value['qnty'];
       $orderItem = new OrderItem;
       $orderItem->order_id = $order->id;
       $orderItem->product_id=$pid;
       $orderItem->quantity=$qnty;
       $orderItem->save()

    }   
           
    return redirect('checkout');
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.