[英]single form for insert and update data
我正在嘗試使用單一形式進行數據插入和數據更新。 但是我不知道該怎么辦。 在我的表單中,我提到了操作方法action="{{action('BookController@create')}}"
,但是我想使用相同的表單進行數據插入和數據更新。
//book.blade.php
<form class="form-horizontal" method="POST" action="{{action('BookController@create')}}" enctype="multipart/form-data">
{{ csrf_field() }}
<div class="row" style="padding-left: 1%;">
<div class="col-md-4">
<div class="form-group">
<label>Book Name</label><span class="required">*</span>
<input type="text" maxlength="100" minlength="3" autofocus="autofocus" autocomplete="off" required="required" name="NBookName" class="form-control"/>
</div>
</div>
<div class="col-md-4">
<div class="form-group" style="padding-left: 5%;">
<label>Unit Price</label><span class="required">*</span>
<input type="text" maxlength="5" required="required" autocomplete="off" runat="server" name="NBookUnitPrice"/>
</div>
<div class="form-group" style="padding-left: 5%;">
<button type="submit" class="btn btn-primary">Submit</button>
</div>
</div>
</div>
</form>
控制器代碼
public function edit($id)
{
$book = Book::find($id);
return view('pages.book',compact('book','id'));
}
路線頁面
// for books
Route::get('/book','BookController@create');
Route::post('/book','BookController@store');
Route::get('/book/{id}','BookController@edit');
我不知道如何進一步處理。
我正在嘗試使用單一表格進行數據插入和數據更新
不,您不會,除非您准備好被另一個需要了解您在此處所做的開發人員殺死。
從Laravel 5.2開始,您遵循Restful資源控制器
這是一個非常重復的解決方案模板
路線
Route::resource('book', 'BookController');
調節器
class BookController extends Controller {
// Display list of your books
public function index() {
$books = Book::all();
return view('books.index', ['books' => $books]);
}
// Show a form to create a book
public function create() {
return view('books.create');
}
// Show a form to edit a book
public function edit(Book $book) {
return view('books.edit', ['book' => $book]);
}
// Store a new book
public function store(Request $request) {
$this->validate($request, [
'book_name' => 'required|unique:books'
]);
$book = new Book();
$book->book_name = $request->book_name;
if($book->save()) {
return redirect()->route('books.edit', $book)
->with('success', 'Successfully added a book'); // You may print this success message
} else {
return redirect()->back()
->withInput()
->with('error', 'Could not add a book'); // You may print this error message
}
}
// Update existing book
public function update(Request $request, Book $book) {
$this->validate($request, [
'book_name' => 'required|unique:books,book_name,'.$book->getKey().',id'
]);
$book->book_name = $request->book_name;
$book->save();
if($book->save()) {
return redirect()->route('books.edit', $book)
->with('success', 'Successfully updated a book'); // You may print this success message
} else {
return redirect()->back()
->withInput()
->with('error', 'Could not updated a book'); // You may print this error message
}
}
// Delete existing book
public function destroy(Book $book) {
if($book->delete()) {
return redirect()->back()
->with('success', 'Successfully deleted a book'); // You may print this success message
} else {
return redirect()->back()->with('error', 'Could not delete a book'); // You may print this error message
}
}
}
刀
// Show all of your books using some foreach look and html table
views/books/index.blade.php
// Create a new book
views/books/index.create.php
// Edit an existing book
views/books/index.edit.php
形式
<!-- Creating a new book (store method) -->
<form action="{{ route('books.store') }}" method="POST">
{{ csrf_field() }}
<input name="book_name" value="{{ old('book_name') ">
<button type="submit">Create</button>
</form>
<!-- Updating an existing book (update method) -->
<form action="{{ route('books.update', $book) }}" method="POST">
{{ csrf_field() }}
{{ method_field('PUT') }}
<input name="book_name" value="{{ old('book_name', $book->book_name) ">
<button type="submit">Update</button>
</form>
<!-- Deleting an existing book (destroy method) -->
<form action="{{ route('books.destroy', $book) }}" method="POST">
{{ csrf_field() }}
{{ method_field('DELETE') }}
<button type="submit">Delete</button>
</form>
沒有測試代碼,但是,坐在您旁邊的開發人員仍然不會因為使用常見的解決方案模式而喪命。
我認為您需要另一條路線:
Route::put('/book/{id}, 'BookController@update')->name('book.update');
在三種方法中,您可以執行以下操作:
public function edit($id)
{
$action = route('book.update', ['id' => $id]);
$book = Book::find($id);
return view('pages.book',compact('book','id', 'action'));
}
並編輯您的表單(我編輯了動作和輸入值)
<form class="form-horizontal" method="POST" action="
{{ $action }}" enctype="multipart/form-data">
{{ csrf_field() }}
<div class="row" style="padding-left: 1%;">
<div class="col-md-4">
<div class="form-group">
<label>Book Name</label><span class="required">*</span>
<input type="text" maxlength="100" minlength="3" autofocus="autofocus" autocomplete="off" required="required" value="{{ $book->name ?? '' }}" name="NBookName" class="form-control"/>
</div>
</div>
<div class="col-md-4">
<div class="form-group" style="padding-left: 5%;">
<label>Unit Price</label><span class="required">*</span>
<input type="text" maxlength="5" required="required" autocomplete="off" runat="server" value="{{ $book->price ?? '' }} name="NBookUnitPrice"/>
</div>
<div class="form-group" style="padding-left: 5%;">
<button type="submit" class="btn btn-primary">Submit</button>
</div>
</div>
不要合並更新,創建,刪除等方法。 另一個請求=>另一個方法。
您可以自由使用一種表單來創建和更新。 如果您遵循CRUD規則,則表單method將是POST(用於創建)和PUT(用於更新),因此請將@method('PUT')
放在表單主體中。
然后您需要創建路由Route::put('/book','BookController@update')->name('book.update');
用於更新和Route::post('/book','BookController@store')->name('book.store');
為商店。 或者只是Route::resource('/book','BookController')
代替所有這些方法。 檢查laravel資源文檔。
你的內下一步BookController
創建update(Request $request)
方法里面更新邏輯和store(Request $request)
方法以存儲邏輯。
而已。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.