laravel-request验证设置为null的图像,并给出错误原因?

7kjnsjlb  于 2021-06-17  发布在  Mysql
关注(0)|答案(3)|浏览(311)

我需要验证我的图像数组作为一个图像和特定的图像文件扩展名只。但是我对图像的请求验证不允许我使用inser null值
例如,我会添加一个内容,不想添加图像。然后图像应该包含null,这就是为什么我需要请求验证为null。但根据我的经验,空值是不允许的,它给了我一个错误,为什么?请帮帮我
这是错误。
未定义变量:升级
这是我的控制器

public function store(Request $request)
    {
        $this->validate($request, [
            'promotion_image' => 'image|nullable|max:1999'
        ]);

        if ($request->has('promotion_image'))
        {   
            //Handle File Upload

            $promotion = [];
            foreach ($request->file('promotion_image') as $key => $file)
            {
                // Get FileName
                $filenameWithExt = $file->getClientOriginalName();
                //Get just filename
                $filename = pathinfo( $filenameWithExt, PATHINFO_FILENAME);
                //Get just extension
                $extension = $file->getClientOriginalExtension();
                //Filename to Store
                $fileNameToStore = $filename.'_'.time().'.'.$extension;
                //Upload Image
                $path = $file->storeAs('public/promotion_images',$fileNameToStore);
                array_push($promotion, $fileNameToStore);
            }

            $fileNameToStore = serialize($promotion);
        }
        else
        {
            $fileNameToStore='noimage.jpg';
        }

        if (count($promotion)) {
            $implodedPromotion = implode(' , ', $promotion);
            $promotionImage = new Promotion;
            $promotionImage->promotion_image = $implodedPromotion;
            $promotionImage->save();

            return redirect('/admin/airlineplus/promotions')->with('success', 'Image Inserted');
        }

        return redirect('/admin/airlineplus/promotions')->with('error', 'Something went wrong.');

        }

这是我的观点

{!! Form::open(['action'=>'Admin\PromotionsController@store', 'method' => 'POST','enctype'=>'multipart/form-data', 'name' => 'add_name', 'id' => 'add_name']) !!}
<div class="form-group">   
    <div class="table-responsive">  
        <table class="table table-bordered" id="dynamic_field">  
           <tr>  
              <td> {{ Form::file('promotion_image[]')}}</td>

              <td>{{ Form::button('', ['class' => 'btn btn-success fa fa-plus-circle', 'id'=>'add','name'=>'add', 'style'=>'font-size:15px;']) }}</td>
           </tr>  
        </table>  
        {{Form::submit('submit', ['class'=>'btn btn-primary', 'name'=>'submit'])}}
    </div> 
</div>  
{!! Form::close() !!}
jv2fixgn

jv2fixgn1#

不确定,但再试一次

'promotion_image' => 'nullable|mimes:jpeg,jpg,png,gif|max:1999'
k5ifujac

k5ifujac2#

你需要申报 $promotion = [] 以上 if ($request->has('promotion_image')) ,而不是在里面。
所以:

public function store(Request $request)
{
    $this->validate($request, [
        'promotion_image' => 'image|nullable|max:1999'
    ]);

    $promotion = [];

    if ($request->has('promotion_image'))
    {   
        //Handle File Upload
hc2pp10m

hc2pp10m3#

这是因为您在表单中选择的文件不是图像。请参阅以下内容,以限制用户仅上载图像。

<input accept=".png, .jpg, jpeg" name="files[]" type="file" multiple>

相关问题