我需要获取为位置表格选择的值,并将其值添加到购物车中。表格中的所有其他信息均已正确收集。这是形式的代码:
<section class="cards">
@foreach($rentals as $rental)
<article class="cards-item">
<figure class="card product">
<div class="card-images">
<img src="{{ asset('img/rentals' . $rental->image) }}" alt="" >
</div>
<figcaption class="card-content">
<h1 class="card-title">{{ $rental->title }}</h1>
<h2 class="card-title product_title">{{ $rental->name }}</h2>
<p class="card-text" style="text-align:left;">{{ $rental->description }}
{{-- <a href="img/pdf/mcelroy-spec/2lc-spec-sheet.pdf" target="_blank">Download Spec Sheet</a> --}}
</p>
<form action="{{ route('cart.store') }} " method="POST">
{{ csrf_field() }}
<fieldset>
<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="bkf">Bakersfield</option>
<option value="ch">Chico</option>
<option value="fsn">Fresno</option>
<option value="hyw">Hayward</option>
<option value="mtc">Manteca</option>
<option value="oak">Oakley</option>
<option value="rwc">Redwood City</option>
<option value="sac">Sacramento</option>
<option value="sal">Salinas</option>
<option value="sj">San Jose</option>
<option value="sjf">San Jose Fusion</option>
<option value="sr">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="pickup-date" value="pickupDate">
<input type="hidden" name="return-date" value="returnDate"> --}}
<input type="hidden" name="location" value="{{ $rental->location }}">
<div class="buttons">
<div class="back">
<button class="primary button" type="submit">Add to Cart</button>
</div>
</div>
</form>
</figcaption>
</figure>
</article>
</section>
</article>
@endforeach
这是购物车视图中的代码:
<section class="main-content">
<section class="content">
<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->pickupDate}} </p>
<p class="rtn-date">Return Date: {{$item->returnDate}}</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>
我的控制器中设置了以下值:
namespace App\Http\Controllers;
use App\Rental;
use Illuminate\Http\Request;
use Session;
use Gloudemans\Shoppingcart\Facades\Cart;
class CartController extends Controller
{
/**
* Display a listing of the resource.
*
* @return \Illuminate\Http\Response
*/
public function index()
{
return view('cart.cart');
}
/**
* Show the form for creating a new resource.
*
* @return \Illuminate\Http\Response
*/
public function create()
{
//
}
/**
* Store a newly created resource in storage.
*
* @param \Illuminate\Http\Request $request
* @return \Illuminate\Http\Response
*/
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)
->associate('App\Rental');
Session::flash('success', 'The item was successfully save!');
return redirect()->route('cart.index');
}
/**
* Display the specified resource.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function show($id)
{
//
}
/**
* Show the form for editing the specified resource.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function edit($id)
{
//
}
/**
* Update the specified resource in storage.
*
* @param \Illuminate\Http\Request $request
* @param int $id
* @return \Illuminate\Http\Response
*/
public function update(Request $request, $id)
{
//
}
/**
* Remove the specified resource from storage.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function destroy($id)
{
Cart::remove($id);
Session::flash('success', 'The item has been removed!');
return redirect()->route('cart.index');
}
我的路线如下:
Route::get('rental_equipment', 'PageContentController@rental_equipment')->name('rental equipment');
Route::resource('rentals','RentalController');
Route::get('/cart', 'CartController@index')->name('cart.index');
Route::post('/cart', 'CartController@store')->name('cart.store');
Route::delete('/cart/{id}', 'CartController@destroy')->name('cart.destroy');
不确定在这里我在做什么错。非常感谢您的帮助。