Laravel 形式 post - MethodNotAllowedHttpException

Laravel form post - MethodNotAllowedHttpException

我正在尝试 post 向数据库发送表单,但出现以下错误

Symfony\Component\HttpKernel\Exception\MethodNotAllowedHttpException thrown with message

该表单已预先填充数据并以 post 路线为目标。通过研究,我发现问题是从 posting 到 get 路线,但我定位的路线是 post.

形式

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

路线

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

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

试试这个

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

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

删除 /{{id}} 后的斜杠并确保您已在模型中定义 table 名称。

在表单操作中删除 url 末尾的斜线:

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

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

然后尝试。