我尝试创建编辑刀片以从数据库中获取旧值。标题成功出现,但图像和正文(文本区域)未出现, 这是我的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>
这是我的contoller连接到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'));
}
任何人都可以帮助我,如何在edit.blade.php中以正确的方式显示图像
答案 0 :(得分:0)
更改此
<img src="{{ $post->image_thumb_url ? $post->image_thumb_url : 'http://placehold.it/200x150&text=no+image' }}" alt="...">
到
<img src="{{ $post->image_thumb_url ? asset('/imgberita/'.$post->image_thumb_url) : 'http://placehold.it/200x150&text=no+image' }}" alt="...">
OR
<img src="{{ $post->image_thumb_url ? asset('public/imgberita/'.$post->image_thumb_url) : 'http://placehold.it/200x150&text=no+image' }}" alt="...">
答案 1 :(得分:0)
对于这一行代码,请进行以下更改:
发件人:
<img src="{{ $post->image_thumb_url ? $post->image_thumb_url : 'http://placehold.it/200x150&text=no+image' }}" alt="...">
收件人:
<img src="{{ $post->imageThumb ?? 'http://placehold.it/200x150&text=no+image' }}" alt="...">
然后将以下函数添加到Post模型中:
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;
}
然后在视图中使用该函数(如果该图像存在),它将返回位置(如果不存在),它将返回null。
$post->imageThumb
如果对此功能有疑问,只需更新该功能内部的public_path() . '/imgberita/'
,希望您有所想法。
对于textare
,您可以在视图中使用old()
函数:
<textarea rows="5" cols="5" class="form-control" name="body" value="{{$post->body}}"></textarea>
例如这样的
<textarea rows="5" cols="5" class="form-control" name="body" value="{{old('body') ?? ''}}"></textarea>
但是请记住,当我们使用old('body')
时我们使用validate()
函数,并且我们通过使用->withInput()
从控制器传递数据是这样的:
return redirect()
->back()
->withInput()
->withErrors($validator);
您还可以使用edit.blade.php
函数直接在with($request)
中传递数据。
如果您的身体为空,则会出现错误,请使用{{$post->body ?? ''}}
。