vendredi 27 avril 2018

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/{id}/" method="POST">
              
              <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=></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=""></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=""></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');
      }
}



from Newest questions tagged laravel-5 - Stack Overflow https://ift.tt/2Huqd8u
via IFTTT

Aucun commentaire:

Enregistrer un commentaire