user9409047
user9409047

Reputation:

Laravel form post - MethodNotAllowedHttpException

I'm attempting to post a form to the database, but I'm getting the following error

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

The form is prepopulated with data and targets a post route. From research, I found out the issue is from posting to a get route, but the route i'm targeting is a post.

The form

 <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 -->

The controller method

  //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();
  }

The tenancy model

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

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

Routes

Routes

Upvotes: 1

Views: 1172

Answers (3)

Jinandra Gupta
Jinandra Gupta

Reputation: 545

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

Try this

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

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

Remove slash after /{{id}} and make sure you have defined table name in model.

Upvotes: 0

Muthu17
Muthu17

Reputation: 1541

Remove Slash from end of your url in form action :

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

Use : /account/tenancy/{{id}} instead of /account/tenancy/{{id}}/

and try.

Upvotes: 1

GabMic
GabMic

Reputation: 1482

You are using the wrong method. As per your routes, change your method in your form to GET.

Upvotes: 0

Related Questions