如何在Laravel中创建输出图像?

lsmepo6l  于 2023-04-07  发布在  其他
关注(0)|答案(1)|浏览(75)

输入数组可以有很多文档,但是每个文档中的照片既可以是一个文件,也可以是一个链接。如何正确检查数组中的每个元素?
我试着这样做,但它并不像我期望的那样工作

<?php

namespace Modules\Documents\Http\Requests;

use Illuminate\Foundation\Http\FormRequest;

class DocumentsStoreRequest extends FormRequest
{
    /**
     * Get the validation rules that apply to the request.
     *
     * @return array
     */
    public function rules()
    {
        return [
            'documents' => 'sometimes|array',
            'documents.*.type' => 'in:certificate,license',
            'documents.*.photo' => match($this->hasFile('documents.*.photo')) {
                true => 'sometimes|mimes:png,jpg,jpeg|max:4096|nullable',
                default => 'sometimes|string|url|nullable'
            }
        ];
    }

    /**
     * Determine if the user is authorized to make this request.
     *
     * @return bool
     */
    public function authorize()
    {
        return true;
    }
}
h7appiyu

h7appiyu1#

我设法克服了这个缺陷

<?php

namespace Modules\Documents\Http\Requests\Job;

use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Http\UploadedFile;
use Illuminate\Support\Facades\Validator;

class DocumentsStoreRequest extends FormRequest
{
    /**
     * Get the validation rules that apply to the request.
     *
     * @return array
     */
    public function rules()
    {
        return [
            'documents' => 'sometimes|array',
            'documents.*.type' => 'in:certificate,license',
            'documents.*.photo' => ['sometimes', 'nullable', function($attribute, $value, $fail) {
                $validator = Validator::make([
                    'field' => $value
                ], [
                    'field' => $value instanceof UploadedFile
                        ? 'mimes:png,jpg,jpeg|max:4096'
                        : 'string|url'
                ]);
                if ($validator->fails()) {
                    $fail($validator->getMessageBag()->getMessages()['field']);
                }
            }]
        ];
    }

    /**
     * Determine if the user is authorized to make this request.
     *
     * @return bool
     */
    public function authorize()
    {
        return true;
    }
}

相关问题