douxing9813
2015-09-02 16: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 16: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条)

相关推荐 更多相似问题