【问题标题】:Laravel edit dropzoneLaravel 编辑拖放区
【发布时间】:2018-02-08 02:20:37
【问题描述】:

我通过DropzoneJS 保存了文件(图像),我可以在任何我喜欢的地方获取它们,但我的问题是如何编辑它们。我转到我的产品编辑页面,我可以看到我的图像,但不知道如何编辑它们,例如(删除其中一些或添加到它们)

这是我获取它们的代码:

public function edit($id)
    {
      $product = Product::findOrFail($id);
      $images = DB::table('images')->where('imageable_id', '=', $product->id)->get();
// rest of codes
}

我在编辑页面中显示我的图像,例如:

@foreach($images as $test)
  <img src="{{url('/')}}/images/{{$test->name}}" alt="test" width="100" height="100">
@endforeach

screenshot

更新

@linktoahref 方法适用于我,如果我想删除每张图片,他建议创建新的上传器,以便在编辑页面上将图片添加到我的产品中,not bad idea totally 但这不是我真正想要的。

我试图实现的是使用 DropZone 本身来返回我的存在 每个产品的图像,并能够删除或添加到它。

我阅读了很多问题、文章等。我知道我必须使用mockFile,我对此一无所知。如果有人可以帮助我完成这项工作,将不胜感激。

谢谢。

【问题讨论】:

    标签: php laravel dropzone.js


    【解决方案1】:

    对于 DELETE,您可以在图像旁边添加一个按钮,该按钮将触发相应控制器的 destroy 方法

    @foreach($images as $test)
        <img src="{{ url('/') }}/images/{{ $test->name }}" alt="test" width="100" height="100">
    
        <a href="#" class="btn btn-danger"
            onclick="event.preventDefault();
                     document.getElementById('image-{{ $test->id }}').submit();">
            DELETE
        </a>
    
        <form id="image-{{ $test->id }}" 
              action="{{ route('images.destroy', ['id' => $test->id]) }}"
              method="POST" style="display: none;">
            {{ csrf_field() }}
            {{ method_field('DELETE') }}
        </form>
    @endforeach
    

    并在您的 ImageController 的 destroy 方法(或您的自定义控制器的方法,确保方法类型相同)中处理图像的删除

    public function destroy(Image $image)
    {
        // Destroy the Image
        Storage::delete($image->name);
    
       // Redirect Back
       return redirect()->back();
    }
    

    【讨论】:

    • 这里出了点问题,不确定是什么,但我在控制台中收到TypeError: document.getElementById(...) is null 错误。
    • 你能检查你的源代码并检查呈现的&lt;a&gt;标签,它是否接收到正确的id?
    • &lt;a href="#" class="btn btn-danger" onclick="event.preventDefault(); document.getElementById('image-28').submit();"&gt; &lt;i class="fa fa-times"&gt;&lt;/i&gt; &lt;/a&gt;
    • 对应的表单是否有相同的id?尝试用下划线替换-
    • 表单没有打印,只是字段。
    猜你喜欢
    • 1970-01-01
    • 2017-07-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-08-10
    • 1970-01-01
    • 2020-06-01
    相关资源
    最近更新 更多