Reputation: 1
I seem to unable to get the data from the database and display it on the edit form and when i checked all the syntax and the codes it doesnt seem to go wrong
My Model:
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Buku extends Model
{
//
}
This is my Controller code for edit:
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);
}
Here is my view in for my edit form, I am using bootstrap modal for the form:
<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">×</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>
Note that i have to use the if isset to actually prevent an error called the Undefined variable error on my $post but it seems to not actually pass the value from the my database to the fields... I was wondering if the isset function is the cause.. I have tried to put the endif at the bottom of the code and the whole modal will not appear when I click on the link
Any help is appreciated..
Upvotes: 0
Views: 532
Reputation: 370
I notice in your controller you use ->with('buku', $post)
. This will send a variable called $buku along to the view, containing the buku that was found using $post = Buku::find($id);
. Change that line to ->with('post', $post)
.
Secondly, I generally use if (!empty($post))
instead of isset
, since empty()
evaluates both if it is set and if it is empty. According to http://php.net/manual/en/function.empty.php: No warning is generated if the variable does not exist. That means empty() is essentially the concise equivalent to !isset($var) || $var == false.
Thirdly, put your @endif
after your Form::close()
.
Fourthly, test to see if $post actually contains data. Either {!! dd($post) !!}
before your if statement or use else
to echo some text.
Hope this helps.
Upvotes: 0
Reputation: 86
Firstly, i suggest to change your controller as follow:
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);
}
I guess, it should solve your problem. Read more about passwing variables to views: https://laravel.com/docs/5.2/views#passing-data-to-views
Upvotes: 3