无法在编辑表单上显示任何内容

时间:2016-08-09 11:52:47

标签: laravel laravel-5 laravel-5.2

我似乎无法从数据库中获取数据并将其显示在编辑表单上,当我检查所有语法和代码时,它似乎不会出错

我的模特:

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Buku extends Model
{
    //
}

这是我的编辑控制器代码:

public function edit($id)
    {
        // find a post in the database and save as a variable
        $post = Buku::find($id);

        // return the view and pass in the variable we previously created
        return view('buku.edit')->with('buku', $post);
    }

以下是我在编辑表单中的视图,我正在使用表单的bootstrap模式:

<div class="modal fade" id="edit_buku" tabindex="-1" role="dialog" aria-labelledby="myModalLabel">
  <div class="modal-dialog" role="document">
    <div class="modal-content">

      <div class="modal-header">
        <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
        <h4 class="modal-title" id="myModalLabel">Edit Buku</h4>
      </div>

      <div class="modal-body">
      @if (isset($post))
        {!! Form::model($post, array('route' => array('buku.update' , $post->id), 'method' => 'PUT')) !!}
        @endif
            <div class="form-group">
              {{ Form::label('judul','Judul') }}
              {{ Form::text('judul', null, ["class" => 'form-control']) }}
            </div>
            <div class="form-group">
              {{ Form::label('deskripsi','Deskripsi') }}
              {{ Form::text('deskripsi', null, ["class" => 'form-control']) }}
            </div>
            <div class="form-group">
             {{ Form::label('pengarang','Pengarang') }}
             {{ Form::text('pengarang', null, ["class" => 'form-control']) }}
            </div>
            <div class="form-group">
             {{ Form::label('tanggal_publikasi','Tanggal Publikasi') }}
             {{ Form::text('tanggal_publikasi', null, ["class" => 'form-control']) }}
            </div>
      </div>
      <div class="modal-footer">
      {{ Form::submit('Edit', array('class' => 'btn btn-success')) }}   
        <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
        {!! Form::close() !!}
      </div>
    </div>
  </div>
</div>

请注意,我必须使用if isset来实际防止在我的$ post上出现一个名为Undefined variable error的错误,但它似乎实际上没有将我的数据库中的值传递给字段...我想知道是否isset函数是原因..我试图将endif放在代码的底部,当我点击链接时不会出现整个模态

感谢任何帮助..

2 个答案:

答案 0 :(得分:3)

首先,我建议您按照以下步骤更改控制器:

public function edit($id)
{
    // find a post in the database and save as a variable
    $post = Buku::find($id);

    // return the view and pass in the variable we previously created
    return view('buku.edit')->with('post', $post);
}

我想,它应该可以解决你的问题。阅读有关passwing变量的更多信息:https://laravel.com/docs/5.2/views#passing-data-to-views

答案 1 :(得分:0)

我在您的控制器中注意到您使用->with('buku', $post)。这将向视图发送一个名为$ buku的变量,其中包含使用$post = Buku::find($id);找到的buku。将该行更改为->with('post', $post)

其次,我通常使用if (!empty($post))而不是isset,因为empty()会评估它是否已设置以及是否为空。根据{{​​3}}:No warning is generated if the variable does not exist. That means empty() is essentially the concise equivalent to !isset($var) || $var == false.

第三,将@endif放在Form::close()之后。

第四,测试$ post是否实际包含数据。在if语句之前{!! dd($post) !!}或使用else来回显一些文本。

希望这有帮助。