Support the ongoing development of Laravel.io →
Requests Input Forms
Last updated 1 year ago.
0

put two radio boxes and set the address model id's as values ?

0

Sorry, but I don't get it. I'm trying to make the user select an already existing address from the db when sending the order, instead of typing it. In the db I have an address table with 5 fields: id, address, city, postal_code and user_id.

I've put a radio box with {!! Form:radio('address', '{{ $address->id}}') !!} but it says that "SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'address' cannot be null (SQL: insert into addresses (address, city, postal_code, user_id, updated_at, created_at) values (, , , 1, 2016-08-22 08:18:08, 2016-08-22 08:18:08))"

How can I send the address fields to the db without typing them ?

Last updated 7 years ago.
0

don't update the address, can you post your controller code?

you should only need to save the addres's id on to the order

Last updated 7 years ago.
0

Here is the chunk that is responsible for saving the address into the db.

public function postOrder(Request $request) { if(!Cart::subtotal()) { return view('cart.index'); }

    $token = $request->input('stripeToken');
    
    $address = Address::firstOrCreate([
        'address'  => $request->input('address'),
        'city'      =>  $request->input('city'),
        'postal_code'   =>  $request->input('postal_code'),
        'user_id'   => Auth::user()->id
    ]);

}

0

Probably the best thing to do would be some UI work as well.

You could have a select box filled with the user's address (probably be nicer as a helper function to return the full address for that record

// this would be placed in your Address model
public function fullAddress()
{
  return "{$this->address},{$this->city},{$this->postal_code}";
}

Then in your html markup you would have an option to select a previous address, or enter a new address (ui decisions would happen here). But your select would be something like

<select name="previousAddress">
  @foreach(Auth::user()->addresses() as $address)
    <option value="{{ $address->id }}">{{ $address->fullAddress() }}</option>
  @endforeach

Then you would need to check if they selected a previous address (or a check/button toggle) and if they did then the posted result is the id according to your addresses table.

Make sense?

0

So here it the selectionbox

           <select name="addressId" class="form-control input-sm">
                @for($num = 1; $num <= count(Auth::user()->addresses); $num++)
                    <option value="{{ $address->id }}" @if($num == $address->id) selected="selected" @endif>{{ $address->fullAddress() }}</option>
                @endfor
                <option value="0">None</option>
            </select>

But in the postOrder() function where I'm saving the order, if there is a $request->input('addressId') when I'm saving the order instead of the $address->id to insert the $request->input('addressId') from the selection box.

The problem is

$address = Address::firstOrCreate([ 'address' => $request->input('address'), 'city' => $request->input('city'), 'postal_code' => $request->input('postal_code'), 'user_id' => Auth::user()->id ]);

I don't know where to check if there is a $request->input('addressId'), and if it is I want to skip the create process.

Here is the code for the whole function

public function postOrder(Request $request) { if(!Cart::subtotal()) { return view('cart.index'); }

    $token = $request->input('stripeToken');

    $address = Address::firstOrCreate([
        'address'  => $request->input('address'),
        'city'      =>  $request->input('city'),
        'postal_code'   =>  $request->input('postal_code'),
        'user_id'   => Auth::user()->id
    ]);
    

    Stripe::setApiKey(env('STRIPE_SK'));

    $cart = Cart::content();

    if(Auth::check()) {
        try {
            $customer = Customer::create([
                'source' => $token,
                'email' =>  Auth::user()->email,
                'metadata'  =>  [
                    "name"    =>  Auth::user()->name,
                ]
            ]);
        } catch(\Stripe\Error\Card $e) {
            return redirect()->route('order')
                ->withErrors($e->getMessage())
                ->withInput();
        }

        $customerID = $customer->id;

        if(!Auth::user()->stripe_customer_id) {
            $user = User::where('email', Auth::user()->email)->update(['stripe_customer_id' => $customerID]);
        }
    } else {
        $customerID = User::where('email', $email)->value('stripe_customer_id');
        $user = User::where('email', $email)->first();
    }
    try {
        foreach($cart as $item) {
                $charge = Charge::create([
                    'amount'    =>  Cart::total() * 100,
                    'currency'  =>  'usd',
                    'customer'  =>  $customerID,
                    'source'    =>  $customer->source,
                    'receipt_email' =>  Auth::user()->email,
                    'metadata'  =>  [
                        'product_name'  => $item->name,
                        'product_price' =>  $item->price
                    ]
                ]);
        }
        
        $order = new Order();

        $order->amount = Cart::total();
        $order->address_id = $address->id;
        $order->payment_id = $charge->id;
    
        Auth::user()->orders()->save($order);
    
    } catch (\Stripe\Error\Card $e) {
        return redirect()->route('order')
            ->withErrors($e->getMessage())
            ->withInput();
    }
   
    foreach($cart as $item) {
        $order->products()->attach($item->id, ['quantity' => $item->qty]);

        $product = Product::where('id', $item->id);
        
        $product->decrement('stock', $item->qty);
    }     
    
    Cart::destroy();

    return redirect()->route('shop.index')
        ->with('successful', 'Your purchase was successful!');
}
Last updated 7 years ago.
0

Sign in to participate in this thread!

Eventy

Your banner here too?

Cosmin89 cosmin89 Joined 22 Aug 2016

Moderators

We'd like to thank these amazing companies for supporting us

Your logo here?

Laravel.io

The Laravel portal for problem solving, knowledge sharing and community building.

© 2024 Laravel.io - All rights reserved.