首页 > 解决方案 > 用于插入和更新数据的单一表单

问题描述

我正在尝试使用单一表单进行数据插入和数据更新。但我不知道该怎么做。在我的表单中,我提到了 action 方法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');

我不知道如何进一步处理它。

标签: phplaravellaravel-5

解决方案


我正在尝试使用单一表单进行数据插入和数据更新

不,你不会,除非你准备好被另一个需要了解你在那里做了什么的开发者杀死。

你从 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>

没有测试代码,但是坐在你旁边的开发人员不会因为使用常见的解决方案模式而杀死你。


推荐阅读