【问题标题】:Laravel 5.8, CRUD when editing image didn't show up (blade)?Laravel 5.8,编辑图像时没有显示 CRUD(刀片)?
【发布时间】:2020-04-12 18:08:27
【问题描述】:

我尝试创建编辑刀片以从数据库中获取旧值。标题成功出现,但图片和正文(textarea)没有出现, 这是我的 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>

这是我的控制器连接到 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中以正确的方式显示图像

【问题讨论】:

  • 请剪切控制器的edit方法,因为您的模型将在该方法中传递视图,然后将传递给update方法进行存储,store方法将用于创建新实体
  • 你是指功能吗?
  • 是的,我指的是函数,但在 OOP 中我们可以称它们为方法

标签: php laravel crud laravel-blade


【解决方案1】:

对于您的这行代码,请更改如下:

发件人:

<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>

但请记住,当我们使用validate() 时,我们使用old('body') 函数,并且我们使用-&gt;withInput() 从控制器传递数据,如下所示:

return redirect()
    ->back()
    ->withInput()
    ->withErrors($validator);

您还可以使用with($request) 函数直接在edit.blade.php 中传递数据。

如果你的 body 是 null 你会得到错误然后使用{{$post-&gt;body ?? ''}}

【讨论】:

  • 完成我使用 image_url ? $post->image_url : 'placehold.it/200x150&text=no+image' }}" alt="...">
  • 公共函数 getImageUrlAttribute($value) { $imageUrl = ""; if ( !is_null($this->image)) { $imagePath = public_path() 。 “/imgberita/”。 $这个->图像; if (file_exists($imagePath)) $imageUrl = asset("imgberita/" . $this->image); } 返回 $imageUrl; }
  • 身体怎么样?
  • @kuntet 我会更新我的答案body textarea
  • 首先,我没有看到您的update 函数,并且在您的store 函数中没有看到任何验证 - 首先在您的视图中使用{{$post-&gt;body ?? ''}}
【解决方案2】:

改变这个

<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="...">

    <img src="{{ $post->image_thumb_url ? asset('public/imgberita/'.$post->image_thumb_url) : 'http://placehold.it/200x150&text=no+image' }}" alt="...">

【讨论】:

  • 你的项目运行目录是什么?
  • 图片已经解决了,但是value body还是不显示
猜你喜欢
  • 2019-12-06
  • 2020-09-13
  • 2022-10-12
  • 2015-02-17
  • 2018-09-05
  • 1970-01-01
  • 1970-01-01
  • 2019-06-12
  • 1970-01-01
相关资源
最近更新 更多