Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel Form PUT method not working

I created a form to update the role model but on clicking on the save button, it does not appear to submit to the controller method. Even the validation errors message does not appear if the name input field is invalid. Below you can find the code used for the form.

Form:

{!! Form::model($role, ['route' => ['roles.update', $role->id], 'method' => 'put']) !!}

   @include('roles.fields')

{!! Form::close() !!}

The fields for the form are:

<!-- Name Field -->
<div class="form-group col-sm-6">
   {!! Form::label('name', 'Name:') !!}
   {!! Form::text('name', null, ['class' => 'form-control']) !!}
</div>

<!-- Display Name Field -->
<div class="form-group col-sm-6">
   {!! Form::label('display_name', 'Display Name:') !!}
   {!! Form::text('display_name', null, ['class' => 'form-control']) !!}
</div>

<!-- Description Field -->
<div class="form-group col-sm-12 col-lg-12">
   {!! Form::label('description', 'Description:') !!}
   {!! Form::textarea('description', null, ['class' => 'form-control',   'rows' => '5']) !!}
</div>

<!-- Submit Field -->
<div class="form-group col-sm-12">
   {!! Form::submit('Save', ['class' => 'btn btn-primary']) !!}
   <a href="{!! route('roles.index') !!}" class="btn btn-  default">Cancel</a>
</div>

Request:

namespace App\Http\Requests;

use App\Http\Requests\Request;
use App\Models\Role;

class UpdateRoleRequest extends Request
{

    /**
     * Determine if the user is authorized to make this request.
     *
     * @return bool
     */
     public function authorize()
     {
         return true;
     }

    /**
     * Get the validation rules that apply to the request.
     *
     * @return array
     */
     public function rules()
     {
         return Role::$rules;
     }
}

Controller:

/**
 * Update the specified Role in storage.
 *
 * @param  int              $id
 * @param UpdateRoleRequest $request
 *
 * @return Response
 */
public function update($id, UpdateRoleRequest $request)
{
    $role = $this->roleRepository->findWithoutFail($id);

    if (empty($role)) {
        Flash::error('Role not found');

        return redirect(route('roles.index'));
    }

    $role = $this->roleRepository->update($request->all(), $id);

    Flash::success('Role updated successfully.');

    return redirect(route('roles.index'));
}

Model:

<?php

namespace App\Models;

use Eloquent as Model;
use Zizaco\Entrust\EntrustRole;
use Illuminate\Database\Eloquent\SoftDeletes;

class Role extends EntrustRole
{
    use SoftDeletes;

    public $table = 'roles';

    protected $dates = ['deleted_at'];

    public $fillable = [
        'name',
        'display_name',
        'description'
    ];

    /**
     * The attributes that should be casted to native types.
     *
     * @var array
     */
    protected $casts = [
        'name' => 'string',
        'display_name' => 'string'
    ];

    /**
     * Validation rules
     *
     * @var array
     */
    public static $rules = [
        'name' => 'required|unique:roles'
    ];
}

Please help if possible. Thanks!

like image 299
Nas Atchia Avatar asked Aug 24 '16 13:08

Nas Atchia


1 Answers

You cant use put as the form method. Read the documentation about method spoofing in laravel

{!! Form::model($role, ['route' => ['roles.update', $role->id], 'method' => 'post']) !!}
   <input type="hidden" name="_method" value="PUT">

   @include('roles.fields')

{!! Form::close() !!}
like image 55
Pawel Bieszczad Avatar answered Sep 22 '22 01:09

Pawel Bieszczad