Laravel 5.1 url 值未传递给控制器 PUT 方法
Laravel 5.1 url value not passing to controller PUT method
我有一个用于更新图书信息的简单表格。
<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>
控制器:
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);
}
}
问题是,它没有传递正确的 $id。它传递一个字符串 {books}
基本上$id = "{books}"
它应该是 url /books/31/edit
中的整数 (31)
在路由中被定义为具有所有可用默认方法的资源
我能做什么?
您需要在表单定义中传递图书 ID,作为 action()
的第二个参数:
<form action="{{ action('BookController@update', ['id' => $book[0]->id]) }}" method="POST" class="form-horizontal">
有关详细信息,请参阅 definition of action()
。
试试这个
<form action="{{ action('BookController@update', ['id'=> 1]) }}" method="POST" class="form-horizontal">
解决方案非常简单:我只需要将 $id 传递给视图,然后按照@EricMakesStuff 的建议将其传递以形成动作..
我有一个用于更新图书信息的简单表格。
<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>
控制器:
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);
}
}
问题是,它没有传递正确的 $id。它传递一个字符串 {books}
基本上$id = "{books}"
它应该是 url /books/31/edit
在路由中被定义为具有所有可用默认方法的资源 我能做什么?
您需要在表单定义中传递图书 ID,作为 action()
的第二个参数:
<form action="{{ action('BookController@update', ['id' => $book[0]->id]) }}" method="POST" class="form-horizontal">
有关详细信息,请参阅 definition of action()
。
试试这个
<form action="{{ action('BookController@update', ['id'=> 1]) }}" method="POST" class="form-horizontal">
解决方案非常简单:我只需要将 $id 传递给视图,然后按照@EricMakesStuff 的建议将其传递以形成动作..