当我尝试保存字段时,Laravel MethodNotAllowedHttpException?

时间:2017-10-08 09:49:52

标签: php laravel laravel-5 laravel-5.2 laravel-5.3

您好我尝试在db ot 2步骤中保存字段但我有MethodNotAllowedHttpException

在我的控制器的第1步中,我在db

中保存了2个字段
 public function storeNumber(Request $request){

    $number = new Number;
    $number->user_id = $user = \Auth::user()->id;
    $number->number = $this->getGeneratedNumber();
    $number = new Number($request->all());
    $number->save();
    return redirect('numbers/{id}/details');
}

视图

<form class="form-horizontal" method="POST" action="{{action('NumberController@storeNumber')}}">
{{ csrf_field() }}

<div class="form-group">
    <div class="col-md-12">
        <button type="submit" class="btn btn-primary btn-block">
            Generate Numbers
        </button>
    </div>
</div>

模型

 class Number extends Model

{     / **      * @var数组      *      * /     protected $ fillable = [         '数'     ]。 }

在第2步我希望在同一个数据库中使用相同的控制器保存另一个字段这是我的另一个存储函数,用于在同一个数据库中存储另一个字段。但是当我试图保存laravel时说MethodNotAllowedHttpException。

public function store(Request $request, $id){

    $number = Number::find($id);
    $number = new Number($request->all());
    $number->save();
    return redirect('numbers');
}

这是我的观点

<form method="post" action="{{action('NumberController@store', $id)}}">
    {{csrf_field()}}
    <input name="_method" type="hidden" value="PATCH">

    <div class="form-group">
        <label for="number" class="control-label">Number</label>
        <input type="text" class="form-control form-control-lg disabled" placeholder="Number" name="number" value="{{$number->number}}">
    </div>
    <div class="form-group">
        <label for="comment" class="control-label">Comment</label>
        <textarea name="comment" class="form-control form-control-lg" cols="30" rows="10">{{$number->comment}}</textarea>
    </div>
    <div class="form-group">
        <label for="accept" class="control-label">Accept</label>
        <input type="radio" name="accept" value="1">Yes<br>
        <input type="radio" name="accept" value="0">No<br>
    </div>
    @if($number->accept == 1)
        <div class="form-group">
            <label for="name" class="control-label">Number</label>
            <input type="text" class="form-control form-control-lg disabled" placeholder="Name" name="name" value="{{$number->name}}">
        </div>
        <div class="form-group">
            <label for="city" class="control-label">City</label>
            <input type="text" class="form-control form-control-lg" placeholder="City" name="city" value="{{$number->city}}">
        </div>
        <div class="form-group">
            <label for="postcode" class="control-label">Postcode</label>
            <input type="number" class="form-control form-control-lg" placeholder="Postcode" name="postcode" value="{{$number->postcode}}">
        </div>
        <div class="form-group">
            <label for="address">Address</label>
            <textarea name="address" class="form-control form-control-lg" cols="30" rows="10">{{$number->address}}</textarea>
        </div>
    @else
        <p>TODO status for NO</p>
    @endif
    <div class="form-group">
        <div class="col-md-12">
            <button type="submit" class="btn btn-default">Finish</button>
        </div>
    </div>
</form>

3 个答案:

答案 0 :(得分:1)

你有一个带有“PATCH”值的“_method”,所以你必须将路线更改为“patch”而不是“post”。

答案 1 :(得分:0)

请按照以下更改刀片更新内容

<form method="post" action="{{action('NumberController@store', $id)}}">

<form method="post" action="{{action('NumberController@update', $id)}}">

答案 2 :(得分:0)

return redirect('numbers/{id}/details'); 

这是不正确的,什么是id?