douxing9813 2015-09-02 08:00
浏览 11
已采纳

Laravel 5.1 url值未传递给控制器​​PUT方法

I have a simple form for updating book info.

<form action="{{ action('BookController@update') }}" method="POST" class="form-horizontal">

  <input type="text" id="title" class="form-control" name="title" placeholder="title" value="{{ $book[0]->title }}">

  <input type="text" id="author" class="form-control" name="author" placeholder="author" value="{{ $book[0]->author }}">
      ......................

  <button type="submit" class="btn btn-primary">Save</button>

  <input type="hidden" name="_method" value="PUT">

  <input type="hidden" name="_token" value="{{ csrf_token() }}" />

</form>

Controller:

public function update(Request $request, $id)
{
    $book = new Book;

    $title      = $request->input('title');
    $author     = $request->input('author');
    $category   = $request->input('category');
    $date       = $request->input('date');

    if ($book->updateBook($id, $title, $author, $category, $date)) {
        return redirect('books')->with('status', 'Successfuly edited!');
    }
    else {
        return dd($id);
    }
}

The problem is, it doesn't pass the right $id. It pass a string {books}

Basically $id = "{books}"

It should be an integer (31) from url /books/31/edit

In routes is defined as a resource with all available default methods What can i do?

展开全部

  • 写回答

3条回答 默认 最新

  • dongtuo3370 2015-09-02 08:13
    关注

    You need to pass the book's ID in the form definition, as the second argument to action():

    <form action="{{ action('BookController@update', ['id' => $book[0]->id]) }}" method="POST" class="form-horizontal">

    See the definition of action() for more information.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)
编辑
预览

报告相同问题?

手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部