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?
答
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.
答
try this
<form action="{{ action('BookController@update', ['id'=> 1]) }}" method="POST" class="form-horizontal">
答
The solution was very simple: i just needed to pass $id to the view and then pass it to form action as @EricMakesStuff suggested..