Laravel如何在数组内部验证数组并创建模型

时间:2020-01-12 13:39:34

标签: laravel

我无法访问对象数组内部的图像访问数组。

*

在以下代码段中也失败:

$this->validate($request, [
   'blocks.*' => 'array|required',
   'blocks.*.position' => 'required|integer',
   'blocks.*.block_title' => 'required|max:100',
   'blocks.*.block_text' => 'required|max:500',
   'block.*.images' => 'required',
   'block.*.images.*' => '|mimes:jpeg,jpg,png,gif,svg|max:10240',
]);

我还尝试了以下方法:

$blocks = $request->blocks;
foreach ($blocks as $block) {

$images = $request->file('blocks.images');

我想创建包含图像的块。

1 个答案:

答案 0 :(得分:0)

在请求中,您应该使用'blocks.*.images''blocks.*.images.*'而不是'block.*.images''block.*.images.*'

要获取图像,您可以尝试:

$data = $request->all();
$blocks = $data['blocks'];
foreach ($blocks as $block) {
   foreach ($block['images'] as $image) {
       // $image should be a file
       $file_name = $image->getClientOriginalName();
       $file = Storage::putFileAs(
                    'path-to-save-file-at', $image, $file_name
               );

   }
}