2017-02-28 105 views
1

我正在學習Laravel。我試圖創建一個表單,列出汽車表中的汽車,如果點擊,發送到另一個基於數據庫查詢的表單,該表單返回所選汽車的數據(由$ modelesc標識)。在Laravel中獲取MethodNotAllowedHttpException錯誤

該表格將數據發送到「訂單」表。使用我現在的代碼,我無法在訂單表上發佈訂單。它得到的信息: 「MethodNotAllowedHttpException在RouteCollection.php線233:」

這是代碼

Web.php

Route::get('catalog', '[email protected]'); 
Route::get('orders/', '[email protected]')->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' => '[email protected]', '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(); 

回答

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) 

The您的隱藏輸入的將被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}', '[email protected]')->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(); 
    ... 
} 
+0

我已經使用了一些建議,並設法使訂單工作。路線變化無效。但現在我有另一個問題。檢查已編輯的第一篇文章。 – Adato

+0

如果你想爲'post'添加一個新的路由'Route :: post('orders','CarController @ functionName') - > name('orders');'定義一個函數來處理這個。 – EddyTheDove

+0

你說我的答案幫助你解決主要問題。現在你正在解決一個不同的問題。如果是這樣,請好好標記我的答案。謝謝:) – EddyTheDove

0

改變你的路線:

Route::get('orders/{model}', '[email protected]')->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 
+0

那麼,我有路線問題,並試圖不改變它。我已經更新了訂單表,並設法讓訂單工作,但現在我有另一個問題。 – Adato

相關問題