I try to create edit blade to get old value from database. the title successfully appears, but the image and body (textarea) didn’t appears, here is some code of my edit.blade.php
<form action="{{ route('blog.update', $post->id) }}" method="PUT" enctype="multipart/form-data" id='post-form'>
@csrf
<div class="form-group">
<label>Isi</label>
<textarea rows="5" cols="5" class="form-control" name="body" value="{{$post->body}}"></textarea>
</div>
<div class="panel panel-flat">
<div class="image-news">
<span>Cover Berita</span>
<ul class="icons-list">
<li><a href="#" data-action="collapse"></a></li>
</ul>
</div>
<div class="category-content">
<div class="form-group ">
<div class="fileinput fileinput-new" data-provides="fileinput">
<div class="fileinput-new thumbnail" style="width: 200px; height: 150px;">
<img src="{{ $post->image_thumb_url ? $post->image_thumb_url : 'http://placehold.it/200x150&text=no+image' }}" alt="...">
</div>
<div class="fileinput-preview fileinput-exists thumbnail" style="max-width: 200px; max-height: 150px;"></div>
<div>
<span class="btn btn-default btn-file"><span class="fileinput-new">Pilih Gambar</span><span class="fileinput-exists">Ganti</span><input type="file" name='image'></span>
<a href="#" class="btn btn-default fileinput-exists" data-dismiss="fileinput">Hapus</a>
</div>
</div>
</div>
</div>
</div>
And here is my contoller connect to edit.blade.php
public function store(Request $request)
{
$post = new Post;
$post->title = $request->get('title');
// $post->excerpt = $request->get('excerpt');
$post->body = $request->get('body');
$post->published_at = $request->get('published_at');
$post->category_id = $request->get('category_id');
$post->author_id = Auth::user()->id;
if ($request->hasFile('image'))
{
$file = $request->file('image');
$image = $file->getClientOriginalName();
$destination = public_path() . '/imgberita/';
$successUploaded = $request->file('image')->move($destination, $file->getClientOriginalName());
if($successUploaded)
{
$extension = $file->getClientOriginalExtension();
$thumbnail = str_replace(".{$extension}", "_thumb.{$extension}", $image);
Image::make($destination . '/' . $image)
->resize(250, 170)
->save($destination . '/' . $thumbnail);
}
$post->image = $image;
}else{
$post->image = 'logo.jpg';
}
$post->save();
return redirect()->route('blog.index')->with('message', 'Berita berhasil dibuat');
}
public function edit($id)
{
$post = Post::findOrFail($id);
return view("backend.blog.edit", compact('post'));
}
Can anyone help me, how to show image the right way in edit.blade.php
Advertisement
Answer
For this line of your code please change as follow:
From:
<img src="{{ $post->image_thumb_url ? $post->image_thumb_url : 'http://placehold.it/200x150&text=no+image' }}" alt="...">
To:
<img src="{{ $post->imageThumb ?? 'http://placehold.it/200x150&text=no+image' }}" alt="...">
Then add the following function to your Post model:
public function imageThumb(){
if($this->image_thumb_url){
if(File::exists(public_path() . '/imgberita/'.$this->image_thumb_url)){
return public_path() . '/imgberita/'.$this->image_thumb_url;
}
}
return null;
}
Then use the function in your view like this if that image exist it will return the location if not exist it will return the null.
$post->imageThumb
If you get issue with this function just update the public_path() . '/imgberita/'
inside the function, hope you get idea.
For textare
you can use the old()
function in your view:
<textarea rows="5" cols="5" class="form-control" name="body" value="{{$post->body}}"></textarea>
For example like this:
<textarea rows="5" cols="5" class="form-control" name="body" value="{{old('body') ?? ''}}"></textarea>
But remember we use the old('body')
function when we use the validate()
and we pass the data from controller by using ->withInput()
like this:
return redirect()
->back()
->withInput()
->withErrors($validator);
Also you can directly pass data in your edit.blade.php
by using with($request)
function.
If your body is null you will get error then use the {{$post->body ?? ''}}
.