【问题标题】:Laravel Dropzonejs file validation in hasmany tablehasmany 表中的 Laravel Dropzonejs 文件验证
【发布时间】:2015-05-27 23:37:17
【问题描述】:

我有一个上传文件的 laravel 应用程序。如果文件存在,我需要检查相关表。我的用户模型 hasmany(assets) 文件信息存在于 assets 表中。我正在尝试将名称从表中提取出来,并根据 dropzone 中上传的文件对其进行验证。

这是我的拖放区脚本:

Dropzone.options.dropzoneUpload = {
paramName: "asset", // The name that will be used to transfer the file
maxFilesize: 2, // MB
accept: function(file, done) {
if (file.name == "{{$filename}}") {
  done("Naha, you don't.");
}
else { done(); }
}
};

为我的上传页面生成视图的函数如下所示

public function index()
{
    $filename = Auth::user()->assets->name;
    return view('upload.index')->with('filename', $filename);
}

文件名变量中的名称返回错误

Undefined property: Illuminate\Database\Eloquent\Collection::$name

我不知道如何从表中访问文件名并使用视图中的脚本对其进行验证。每个用户在表中有多个文件名。

Pre dropzone 我可以在我的控制器中使用它

foreach($userAssets as $asset)
        { 
            if($asset->name == $filename)
            { 
                return redirect('upload')->with('errornotice', 'A file with that name already exist');
            } 
        }

但这在脚本中不起作用。

【问题讨论】:

    标签: php mysql laravel laravel-5


    【解决方案1】:

    既然你自己说你的User 有很多Assets,那么assets->name 到底应该打印什么? name 其中Asset?因为你有很多这些并且user->asset 正在返回一个集合,你可能会迭代那个集合,从所有资产names 中构造一个 JSON 数组并将其注入到你的 JS 中。然后执行 JavaScript for/each 来检查数组的所有元素。不过,这是一个弱验证,任何用户都可以在提交之前编辑 JS。如果文件名不存在对您很重要,请考虑某种服务器端验证。

    【讨论】:

      猜你喜欢
      • 2016-03-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-05-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多