Laravel表单post - MethodNotAllowedHttpException

时间:2018-04-27 11:32:27

标签: laravel laravel-5 eloquent laravel-5.6

我正在尝试将表单发布到数据库,但是我收到以下错误

  

Symfony \ Component \ HttpKernel \ Exception \ MethodNotAllowedHttpException抛出消息

表格预填充了数据并以后期路线为目标。从研究中我发现问题是从发布到获取路线,但我所针对的路线是一个帖子。

表格

 <form action="/account/tenancy/{{$user->id}/" method="POST">
              {{ csrf_field() }}
              <div class="row">
                <div class="col-md-6">
                  <label for="property_address">Property Address</label>
                </div> <!-- ./col6 -->
              </div> <!-- ./ row-6 -->
              <div class="row">
                <div class="col-md-10">
                  <select class="form-control" id="property_address" name="property_address">
                    <!--Gets all counties from DB -->
                    @foreach ($properties as $property)
                      <option value={{$property->id}}>{{$property->address . ', ' . $property->town . ', ' . $property->county}}</option>
                    @endforeach
                  </select>
                </div> <!-- ./ col-6-->
              </div> <!-- ./ row-5  -->
              <div class="row mt-2">
                <div class="col-md-6">
                  <label for="landlord-name">Landlord Name</label>
                </div> <!-- ./col=6 -->
              </div> <!-- ./ row-4-->
              <div class="row">
                <div class="col-md-6">
                  <select class="form-control" name="landlord-name">
                    <option value="{{Auth::user()->name}}">{{Auth::user()->name}}</option>
                  </select>
                </div> <!-- ./ row 3-->
              </div> <!-- ./col-3 -->
              <div class="row mt-2">
                <div class="col-md-6">
                  <label for="tenand-name">Tenant Name</label>
                </div> <!-- ./col=6 -->
              </div> <!-- ./ row-4-->
              <div class="row">
                <div class="col-md-6">
                  <select class="form-control" name="tenant-name">
                    <option value="{{$user->name}}">{{$user->name}}</option>
                  </select>
                </div> <!-- ./ row 3-->
              </div> <!-- ./col-3 -->
              <button class="mt-2 btn btn-primary" type="submit">Create Tenancy</button>
            </form> <!-- ./form -->

控制器方法

  //Renders Form
  public function create($id){
    $user = User::where('id', $id)->first();
    $properties = PropertyAdvert::where('user_id', Auth::id())->get();

    return view('/pages/account/tenancy/create', compact('user', 'properties'));
  }

  //Stores data
  public function store(Request $request){
    $Tenancy = Tenancy::create([
      'tenant_id' => $request->user_id,
      'landlord_id' => Auth::id(),
      'property_address' => $request->property_address
    ]);

    return back();
  }

租赁模式

class Tenancy extends Model
{
    protected $fillable = ['tenant_id', 'landlord_id', 'property_address', 'accepted'];

    public function user(){
        return $this->belongsTo('App\User');
      }
}

路线

Routes

3 个答案:

答案 0 :(得分:1)

在表单操作中删除网址末尾的斜杠:

<form action="/account/tenancy/{{id}}" method="POST">

使用:/account/tenancy/{{id}}代替/account/tenancy/{{id}}/

然后试试。

答案 1 :(得分:0)

您使用的是错误的方法。根据您的路线,将表单中的方法更改为GET。

答案 2 :(得分:0)

<form action="/account/tenancy/{{id}}" method="POST">

试试这个

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Tenancy extends Model
{
    protected $table = 'tenancy';
    --------------------
    --------------------
}

在/ {{id}}之后删除斜杠,并确保在模型中定义了表名。