当我尝试在Laravel中编辑时,值显示为空白

时间:2019-12-22 14:14:27

标签: laravel

我想在laravel中编辑

在我的控制器中,我有这个

- name: install packages
  package:
    name: "{{ packages }}"
    state: present
  vars:
    packages:
      - firefox
      - gimp
      - inkscape
      - texlive-full
      ...
      - steam
  become: yes

在我所有图书的索引视图中,都有这个

public function edit(Book $book)
{
    return view ('admin.book.edit')->with('book', $book)->with('authors', Author::all())->with('categories', Category::all());
}

我的编辑视图就是这样

    <tbody>
        @foreach($books as $book)
         ...
             <td> {{ $book -> name }} </td>
            <td>  
            <a href="{{ route('admin.book.edit',$book->id) }}">
                <button class="btn btn-success">Edit</button>
            </a>
            </td>
            ...

在我的web.php中

<form action="#" method="POST" enctype="multipart/form-data">
    @csrf
    <div class="form-group">
        <label for="name">Book Name</label>
        <input type="text" class="form-control" name="name" id="name" value="{{ $book->name }}" >
    </div>

    <div class="form-group">
        <label for="about">About the Book </label>
        <textarea name="about" id="about" cols="5" rows="5" class="form-control">{{ $book->about }}</textarea>
    </div>

我的表架构

Route::get('/admin/book/edit', 'BooksController@edit')->name('admin.book.edit');

我本以为这个Schema::create('books', function (Blueprint $table) { $table->bigIncrements('id'); $table->string('name'); $table->text('about'); ... }); 将返回值,但返回空白。可能我在这里做错了。

2 个答案:

答案 0 :(得分:1)

嗨,您没有在route Model绑定中定义:

将其更改为,因此Laravel知道您在路由中传递的ID应该返回Book模型。您在编辑功能edit(Book $book)

中使用的
Route::get('/admin/book/edit/{book}', 'BooksController@edit')->name('admin.book.edit');

也将look放入Resource Controllers

Route::resource('books', 'BooksController');

这将生成您需要的所有路线view/edit/update/delete检查运行php artisan route:list的路线

阅读Docs了解更多信息

答案 1 :(得分:1)

他们是我路线中的错误,所以我改成这个

Route::get('/admin/book/edit/{book}', 'BooksController@edit')->name('admin.book.edit');