将日期从表单传递到刀片视图

时间:2018-08-23 02:50:07

标签: php laravel forms laravel-5 cart

我有一个购物车,该购物车的提货和退货日期选项需要选择。选择它们后,我无法将它们中的任何一个回显到购物车视图中。我正在使用Laravel Shopping Cart库来构建购物车,其中包含用于附加选项的数组。我已经将值传递到那里以传递到视图,但是它似乎没有用。

这是表格标记:

<form action="{{ route('cart.store') }} " method="POST">
                    {{ csrf_field() }}
                    <fieldset>
                        <div class="formrow" style="margin-right: -10;">
                            <div class="formitem col1" style="margin-right: -10">
                                <label class="label req" for="pickupDate" style="float:left;">Pick Up Date</label>
                                <input type="date" name="pickupDate" id="pickupDate" class="pickupDate"/>
                            </div>
                        </div>
                        <div class="formrow" style="margin-right: -10;">
                            <div class="formitem col1" style="margin-right: -10">
                                <label class="label req" for="returnDate" style="float:left;">Return Date</label>
                                <input type="date" name="returnDate" id="returnDate" class="return" />
                            </div>
                        </div>
                        <div class="formrow" style="margin-right: -10;">
                            <div class="formitem col1of2" style="float: left;">
                                <label class="label" for="location" style="float:left;">Pick Up Location</label>
                                <select name="location" id="location" class="location">
                                    <option>please choose</option>
                                    <option value="bakersfield">Bakersfield</option>
                                    <option value="chico">Chico</option>
                                    <option value="fresno">Fresno</option>
                                    <option value="hayward">Hayward</option>
                                    <option value="manteca">Manteca</option>
                                    <option value="oakley">Oakley</option>
                                    <option value="redwood_city">Redwood City</option>
                                    <option value="sacramento">Sacramento</option>
                                    <option value="salinas">Salinas</option>
                                    <option value="san_jose">San Jose</option>
                                    <option value="san_jose_fusion">San Jose Fusion</option>
                                    <option value="santa_rosa">Santa Rosa</option>
                                </select>
                            </div>
                        </div>
                    </fieldset>
                    <input type="hidden" name="id" value="{{ $rental->id }}">
                    <input type="hidden" name="title" value="{{ $rental->title }}">
                    <input type="hidden" name="pickupDate" value="{{ $rental->pickupDate }}">
                    <input type="hidden" name="returnDate" value="returnDate">
                    <div class="buttons">
                        <div class="back">
                            <button class="primary button" type="submit">Add to Cart</button>
                        </div>
                    </div>
                </form>

这是购物车视图:

<article>
        @if(session()->has('success_message'))
        <div class="alert alert-success">
            {{ session()->get('success_message') }}
        </div>
        @endif
        <h1>Shopping Cart</h1>
        @if(count($errors) > 0)
        <div class="alert alert-danger">
            <ul>
                @foreach ($errors->all() as $error)
                <li>{{ $error }}</li>
                @endforeach
            </ul>
        </div>
        @endif
        @if (Cart::count() > 0)
        <h2>{{ Cart::count() }} item(s) in Shopping Cart</h2>
        <div>
            <div>
                @foreach (Cart::content() as $item)
                <fieldset>
                    <article class="js-cart-product">
                        <p class="prod-title">Name: {{$item->model->name}} </p>
                        <p class="pu-date">Pick up date: {{ ($item->options->has('pickupDate') ? $row->options->pickup : '') }} </p>
                        <p class="rtn-date">Return Date: {{ ($item->options->has('returnDate') ? $row->options->pickup : '') }}</p>
                        <p class="loc">Location: {{$item->location}}</p>
                        <form action="{{ route('cart.destroy', $item->rowId)}}" method="POST">
                            {{csrf_field()}}
                            {{method_field('DELETE')}}
                            <div class="buttons">
                                <div class="back">
                                    <button class="primary button" type="submit">Delete Item</button>
                                </div>
                            </div>
                            <!-- <div class="cart__footer">
                                <p class="cart__text">
                                    <a class="button" href="#" title="Buy products">
                                        Check Out
                                    </a>
                                </p>
                            </div> -->
                        </form>
                    </article>
                </fieldset>
                @endforeach
            </div>
        </div>
        @else
        <h3>No items in Cart!</h3>
        <a href="{{ route('rental equipment')}} ">Return to Rental Equipment</a>
        @endif
    </div>
</article>

这是控制器的创建项目部分:

public function store(Request $request)
{

    $duplicates = Cart::search(function ($cartItem, $rowId) use ($request) {
        return $cartItem->id === $request->id;
    });
    if ($duplicates->isNotEmpty()) {
        return redirect()->route('cart.index')->with('success_message', 'Item is already in your cart!');
    }

     $this->validate($request, array(
        'location'=>'required',
    ));

    Cart::add($request->id, $request->title, 1, $request->location, $options = ['pickup' => 'pickupDate', 'returnDate' => 'returnDate'])
        ->associate('App\Rental');

    Session::flash('success', 'The item was successfully save!');

    return redirect()->route('cart.index');
}

1 个答案:

答案 0 :(得分:0)

从表单刀片中

<form action="{{ route('cart.store') }} " method="POST">
   {{ csrf_field() }}
   <fieldset>
      <div class="formrow" style="margin-right: -10;">
         <div class="formitem col1" style="margin-right: -10">
            <label class="label req" for="pickupDate" style="float:left;">
               Pick Up Date
            </label>
            <input type="date" name="pickupDate" id="pickupDate" class="pickupDate"/>
         </div>
       </div>
       <div class="formrow" style="margin-right: -10;">
          <div class="formitem col1" style="margin-right: -10">
             <label class="label req" for="returnDate" style="float:left;">Return Date</label>
             <input type="date" name="returnDate" id="returnDate" class="return" />
          </div>
       </div>

然后将其放置在主刀片/应用刀片的底部

<script>
$(function() {
    $("#pickupDate" ).datepicker({dateFormat: 'dd/mm/yyyy'});
});
</script>

<script type="text/javascript">
$(function() {
    $("#returnDate").datepicker({dateFormat: 'dd/mm/yyyy'});
});
</script>

确保包括这些必需的文件jquery.min.js,如果使用bootstrap,则需要bootstrap.min.js,bootstrap-datepicker.min.js

请让我知道它是否有效