2017-02-28 3 views
1

Laravel을 배우고 있습니다. 자동차 테이블에있는 자동차를 나열하는 양식을 만들려고합니다. 클릭하면 선택한 모델 ($ modelesc로 식별)의 데이터를 반환하는 DB 쿼리를 기반으로 다른 양식으로 보냅니다.Lolavel에서 MethodNotAllowedHttpException 오류가 발생했습니다.

이 양식은 데이터를 "주문"테이블로 보냅니다. 지금 가지고있는 코드를 사용하면 주문 테이블에 주문을 게시 할 수 없습니다. "RouteCollection.php 라인 (233)에 MethodNotAllowedHttpException :"

이 코드

이 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 
,210

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) 
를 작동

숨겨진 입력의은 request->modelesc에 의해 읽혀지고 http://mysite.dev/orders?modelesc=toyota과 같은 URL을 사용하게됩니다.

조언

만 숨겨진 입력을 제출하려면이 양식을 사용하여. 사용자 입력이 없습니다. 간단한 앵커 <a></a>을 사용하는 것이 더 쉬울 것 같습니다.

@foreach($cars as $car) 
    <a href="/orders?{{ $car->Model }}">{{ $car->Model }}</a> 
@endforeach 

같은 결과입니다.

예쁜 URL로 멋지게 만드십시오.경로의

Route::get('orders/{modelesc}', '[email protected]')->name('orders'); 

정의와라는 이름의 경로

<a href="{{ route('orders', $car->Model) }}">{{ $car->Model }}</a> 

을 그리고 기능

public function orders($modelesc) 
{ 
    $cars = DB::table('cars')->where('Model', $modelesc)->get(); 
    ... 
} 
+0

나는 당신의 제안을 사용하고 명령을 내릴 수 있었다. 경로가 변경되지 않았습니다. 하지만 지금은 또 다른 문제가 있습니다. 수정 된 첫 번째 게시물을 확인하십시오. – Adato

+0

'Route :: post ('orders', 'CarController @ functionName') -> name ('orders'); 게시물을위한 새 경로를 추가하려면 'POST'를하고 그 일을 처리 할 함수를 정의하십시오 . – EddyTheDove

+0

당신은 내 대답이 당신이 주요 질문을하는 데 도움이된다고 말했다. 이제 다른 질문을 해결하고 있습니다. 그렇다면 제 대답을 친절하게 표시해주십시오. 고마워 :) – EddyTheDove

0

변화 경로와 앵커

<a href="/orders/{{ $car->Model }}">{{ $car->Model }}</a> 

로 변경 :

당신이 당신의 주문 페이지

에 이동을위한 양식을 필요가 없습니다 귀하의 경우 귀하의 Catalog.blade.php

을 변경 이제

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')); 
    } 

과 :

Route::get('orders/{model}', '[email protected]')->name('orders'); 

이 이후에 컨트롤러 변경

@foreach($cars as $car) 
    <a href="{{route('order', ['model' => $car->Model])}}">{{ $car->Model }}</a> 
@endforeach 
+0

글쎄, 내가 경로와 문제가 있었고, 그것을 변경하지 않으려 고 노력. 주문 테이블을 업데이트하고 명령을 작동하도록 관리했지만 지금은 또 다른 문제가 있습니다. – Adato

관련 문제