我正在学习Laravel。我正在尝试创建一个列出汽车表中汽车的表单,如果单击,则发送到另一个表单,该表单基于DB查询返回所选汽车的数据(由$ modelesc标识)。
此表单将数据发送到“订单”表。使用我现在的代码,我无法在订单表上发布订单。它得到消息:“RouteCollection.php第233行中的MethodNotAllowedHttpException:”
这是代码
Web.php
Route::get('catalog', 'carController@catalog');
Route::get('orders/', 'CarController@orders')->name('orders');
CarController
function catalog() {
$cars = DB::table('cars')->get();
return view('catalog', compact('cars'));
}
function orders(Request $request) {
$modelesc = $request->modelesc;
$cars = DB::table('cars')->where('Model', $modelesc)->get();
$colours = DB::table('colours')->get()->pluck('Colour');
return view('orders', compact('cars', 'colours'));
}
Catalog.blade.php
@foreach($cars as $car)
{!! Form::open(array('action' => 'CarController@orders', 'method' => 'GET')) !!}
{!! Form::hidden('$modelesc', $car->Model) !!}
{!! Form::submit($car->Model) !!}
{!! Form::close() !!}
@endforeach
Orders.blade.php
@foreach($cars as $car)
{!! Form::open(['method' => 'POST']) !!}
<a href="{{route('orders', $car->Model) }}">{{ $car->Model }}</a>
{!! Form::hidden('users_id', Auth::user()->id) !!}
{!! Form::hidden('Fabrication_date', date('Y-m-d')) !!}
{!! Form::select('Colour_id', $colours) !!}
{!! Form::hidden('Order_status_id', '1') !!}
{!! Form::submit('Ok') !!}
{!! Form::close() !!}
@endforeach
表格订单具有以下结构:
$table->increments('id');
$table->integer('users_id')->unsigned();
$table->foreign('users_id')->references('id')->on('users')->onDelete('cascade')->onUpdate('cascade');
$table->string('Model');
$table->date('Fabrication_date');
$table->integer('Colour_id')->unsigned();
$table->foreign('Colour_id')->references('id')->on('colours')->onDelete('cascade')->onUpdate('cascade');
$table->integer('Order_status_id')->unsigned();
$table->foreign('Order_status_id')->references('id')->on('order_status')->onDelete('cascade')->onUpdate('cascade');
$table->timestamps();
答案 0 :(得分:1)
更新您的订单功能
public function orders(Request $request)
{
$modelesc = $request->modelesc;
$cars = DB::table('cars')->where('Model', $modelesc)->get();
...
}
Catalog.blade.php
Form::hidden('modelesc', $car->Model)
隐藏输入的value
将由request->modelesc
读取,您最终会得到一个类似于http://mysite.dev/orders?modelesc=toyota
的网址。
一些建议
您使用表单仅提交隐藏的输入。没有用户输入。在我看来,使用简单的锚<a></a>
会更容易。
@foreach($cars as $car)
<a href="/orders?{{ $car->Model }}">{{ $car->Model }}</a>
@endforeach
同样的结果。
使用漂亮的网址让它变得更好。更改路线定义
Route::get('orders/{modelesc}', 'CarController@orders')->name('orders');
锚点
<a href="/orders/{{ $car->Model }}">{{ $car->Model }}</a>
使用命名路线
<a href="{{ route('orders', $car->Model) }}">{{ $car->Model }}</a>
功能
public function orders($modelesc)
{
$cars = DB::table('cars')->where('Model', $modelesc)->get();
...
}
答案 1 :(得分:0)
将您的路线更改为:
Route::get('orders/{model}', 'CarController@orders')->name('orders');
此后将您的控制器更改为:
public function orders($model) {
$cars = DB::table('cars')->where('Model', $model)->get();
if(!count($cars)){
abort(404);
}
$colours = DB::table('colours')->get()->pluck('Colour');
//$status = DB::select('select * from order_status where id = ?', [1])->get(); //this variable is never used
return view('orders', compact('cars', 'colours'));
}
现在更改您的Catalog.blade.php
在您的情况下,您不需要在订单页面中输入表单
@foreach($cars as $car)
<a href="{{route('order', ['model' => $car->Model])}}">{{ $car->Model }}</a>
@endforeach