Adding order item for an order - cruds of order item

Step 01: Adding create() method inside OrderItemController.php fetching query string orderId with request->query object

public function create( Request $request ) { //dd($request->query('id'));
	$meta_title = 'Add Order Item';
	$orderId    = '';

	$products  = Product::orderBy( 'sequence', 'asc' )->get(); 
	$profiles = Profile::orderBy( 'sequence', 'asc' )->get(); 
	$fwidths = FrameWidth::orderBy( 'sequence', 'asc' )->get(); 

	if ( !empty( $request->query( 'order_id' ) ) ) {
		$orderId = $request->query( 'order_id' );
	}

	return view( 'order_items.create', compact( 'meta_title', 'orderId', 'products', 'profiles', 'fwidths' ) );
} 

Step 02: Creating create.blade.php under resources/views/order_items putting form input values like below

@extends('layouts.app')

@section('content')

<div class="container">
    <div class="row justify-content-center">
        <div class="col-md-8">
        <h2 class="float-start">Add Order Item</h2>
        <div class="float-end">
            <a class="btn btn-sm btn-primary" href="{{ route('order_items.index').'?order_id='.$orderId }}"> Back to order items</a>
        </div>
        <div class="clearfix"></div>
        <div class="card ">
            <div class="card-body">
            <form method="post" action="{{ route('order_items.store').'?order_id='.$orderId }}" id="orderCrfrm" method="post"> 
                @csrf 
                <input type="hidden" name="url_process_cost" value="{{ url('').'/order_items/cal_cost' }}" />              
                <div class="form-group mb-2">                
                    <label for="title">Choose Product &nbsp;<span class="text-danger">*</span></label>
                    <br>
                    <select name="product" class="form-control product-select2 ">
                        <option value="">--</option>
                        @foreach ($products as $product)
                            @php
                                $selected = $product->id == old('product') ? 'selected' : '';
                            @endphp
                            <option value="{{ $product->id }}" {{ $selected }}>{{ $product->name }} -
                                ${{ number_format($product->cost, 2) }} / sf</option>
                        @endforeach
                    </select>
                    @if ($errors->has('product'))
                        <span class="text-danger">{{ $errors->first('product') }}</span>
                    @endif
                </div>   
                <div class="form-group mb-2">                
                    <label for="title">Choose Profile &nbsp;<span class="text-danger">*</span></label>
                    <br>
                    <select name="profile_id" class="form-control">
                        <option value="">--</option>
                        @foreach ($profiles as $profiler)
                            @php
                                $selected = $profiler->id == old('profile_id') ? 'selected' : '';
                            @endphp
                            <option value="{{ $profiler->id }}" {{ $selected }}>{{ $profiler->name }} </option>
                        @endforeach
                    </select>
                    @if ($errors->has('profile_id'))
                        <span class="text-danger">{{ $errors->first('profile_id') }}</span>
                    @endif
                </div>  
                <div class="form-group mb-2">                
                    <label for="title">Frame Width &nbsp;<span class="text-danger">*</span></label>
                    <br>
                    <select name="frame_width" class="form-control">
                        <option value="">--</option>
                        @foreach ($fwidths as $frame_width)
                            @php
                                $selected = $frame_width->id == old('frame_width') ? 'selected' : '';
                            @endphp
                            <option value="{{ $frame_width->id }}" {{ $selected }}> {{ $frame_width->name }} - {{ $frame_width->percentage }}% </option>
                        @endforeach
                    </select>
                    @if ($errors->has('frame_width'))
                        <span class="text-danger">{{ $errors->first('frame_width') }}</span>
                    @endif
                </div>   
                <div class="form-group mb-2">                
                    <label for="title">Width ( In Inches )&nbsp;<span class="text-danger">*</span></label>
                    <br>
                    <input name="width" class="form-control" placeholder="width" title="width" type="text" id="width" value="{{ old('width') }}" /> 
                    @if ($errors->has('width')) 
                        <span class="text-danger">{{ $errors->first('width') }}</span> 
                    @endif
                </div> 
                <div class="form-group mb-2">                
                    <label for="title">Height ( In Inches )&nbsp;<span class="text-danger">*</span></label>
                    <br>
                    <input name="height" class="form-control" placeholder="height" title="height" type="text" id="height" value="{{ old('height') }}" /> 
                    @if ($errors->has('height')) 
                        <span class="text-danger">{{ $errors->first('height') }}</span> 
                    @endif
                </div>                         
                <div class="form-group mb-2">                
                    <label for="title">Total Square Feet </label>
                    <br>
                    <input name="total_square_feet" class="form-control" style="background-color:darkgray;" placeholder="total square feet" title="total square feet" type="text" readonly  /> 
                </div>       
                <div class="form-group mb-2">                
                    <label for="title">Quantity &nbsp;<span class="text-danger">*</span></label>
                    <br>
                    <input name="quantity" class="form-control" placeholder="quantity" title="quantity" type="text" id="quantity" value="{{ old('quantity') }}" /> 
                    @if ($errors->has('quantity')) 
                        <span class="text-danger">{{ $errors->first('quantity') }}</span> 
                    @endif
                </div> 
                <div class="form-group mb-2">                
                    <label for="title">Cost </label>
                    <br>
                    <input name="product_cost" class="form-control" style="background-color:darkgray;" placeholder="cost" title="cost" type="text" readonly  /> 
                </div>    
                <div class="form-group mb-2">                
                    <label for="title">Additional Request</label>
                    <br>
                    <textarea class="form-control" rows="3" name="additional_request" placeholder="Additional Request">{{ old('additional_request') }}</textarea>
                </div>                                                                                                                                                                               
                <input class="btn btn-sm btn-primary" class="form-control" type="submit" name="action" value="Add to Cart">
            </form>
            </div>
        </div>
        </div>
    </div>
</div>
    

@endsection

Please note, necessary js codes for select2 dropdown, total sq feet and order item price calculation ( with product, quantity, total_sq_feet and frame_with ) with ajax are already created in this step

Step 03: Back in OrderItemController adding store() method like below for saving order items and redirecting to oreview page after addition

// at the top 
use App\Models\OrderItem;

public function store( Request $request ) {

	if ( !empty( $request->query( 'order_id' ) ) ) {
		$orderId = $request->query( 'order_id' );
	}

	$request->validate( [ 'product' => 'required', 'profile_id' => 'required', 'frame_width' => 'required',
	'width'  => 'required', 'height' => 'required', 'quantity' => 'required'] );

	$item = new OrderItem();
	$item->create([
		'order_id' => $orderId,
		'product_id' => $request->post( 'product' ),
		'profile_id' => $request->post( 'profile_id' ),
		'frame_width_id' => $request->post( 'frame_width' ),
		'width' => $request->post( 'width' ),
		'height' => $request->post( 'height' ),
		'total_square_feet' => $item->calculateTotSqFeet( $request->post( 'width' ), $request->post( 'height' ) ),
		'quantity' => $request->post( 'quantity' ),
		'cost' => Product::where('id',$request->post( 'product' ))->first()->cost,
		'additional_request' => $request->post( 'additional_request' ) ?? '',
	]);         

	return redirect()->route( 'order_items.index', 'order_id='.$orderId )->with( 'success', 'Item added to Cart successfully.' );
} 

Related Posts


Building mini ecommerce in Laravel

Listing rows of users - crud

Adding user data - crud of users

Editing user data - crud of users

Deleting user data - crud of users

Listing rows of products - crud

Listing rows of profiles - crud

Listing rows of orders - crud

Listing rows of order items - crud