Php 用于插入和更新数据的单表单

Php 用于插入和更新数据的单表单,php,laravel,laravel-5,Php,Laravel,Laravel 5,我尝试使用单一表单进行数据插入和数据更新。但我不知道怎样才能做到。 在我的表格中,我提到了action方法action=“{{action('BookController@create“)}”,但我想使用相同的表单进行数据插入和数据更新 //book.blade.php <form class="form-horizontal" method="POST" action="{{action('BookController@create')}}" enctype="multipart/for

我尝试使用单一表单进行数据插入和数据更新。但我不知道怎样才能做到。 在我的表格中,我提到了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>
路由页面

// for books
Route::get('/book','BookController@create');
Route::post('/book','BookController@store');
Route::get('/book/{id}','BookController@edit');

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

我想您需要另一条路线:

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'));

}
和编辑表单(我编辑了操作和输入值)


{{csrf_field()}}
书名*
单价*
我尝试使用单一表单进行数据插入和数据更新

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

你从拉威尔5.2开始就遵循这个规则

这是一个非常重复的解决方案模板

路线

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
        }
    }
}
刀片

形式


{{csrf_field()}}
创造
{{csrf_field()}}
{{method_字段('PUT')}
更新
{{csrf_field()}}
{{method_field('DELETE')}
删除

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

不要合并更新、创建、删除等方法。另一个请求=>另一个方法

您可以自由使用一个表单进行创建和更新。如果您遵循CRUD规则,那么您的表单meethod将被发布以供创建,并被放置以供更新,因此请将
@method('PUT')
放在您的表单正文中

然后您需要创建route
route::put('/book','BookController@update')->name('book.update')用于更新和
路由::post('/book','BookController@store')->名称('book.store')用于存储。或者只是
Route::resource('/book','BookController')
而不是所有这些方法。查看laravel资源文档

BookController
create
update(Request$Request)
方法和
store(Request$Request)
方法中的下一步是使用update逻辑


就是这样。

您可以将表单设置为a,并将操作设置为插槽,这样您就可以使用任何想要的操作调用它设置错误
未定义变量:action(视图:C:\xampp\htdocs\laravel\lsapp\resources\views\pages\book.blade.php)
@nischallin将操作变量添加到表单返回的每个控制器方法中
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>