在Laravel中插入空字符串或空图像

时间:2018-11-28 07:35:22

标签: php laravel laravel-5 laravel-filesystem

我无法在数据库中插入字符串或null值,从而导致以下错误:

  

SQLSTATE [HY000]:常规错误:1364字段'paxsafety_video'   没有默认值(SQL:插入pax_safeties   (paxsafety_imageupdated_atcreated_at)个值   (Desert_1543390457.jpg,2018-11-28 15:34:17,2018-11-28 15:34:17))

控制器

     $this->validate($request, [
        'paxsafety_image.*' => 'nullable|image|mimes:jpeg,jpg,png',
        'paxsafety_video.*' => 'nullable|mimes:mp4,mov,ogg | max:20000'
    ]);
    $paxSafety = [];
    $paxSafetyVideo = [];
    if ($request->has('paxsafety_image') && $request->has('paxsafety_video'))
    {   
        //Handle File Upload


        foreach ($request->file('paxsafety_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/paxsafety_folder',$fileNameToStore);
            array_push($paxSafety, $fileNameToStore);
        }



        foreach ($request->file('paxsafety_video') as $key => $file)
        {
            // Get FileName
            $filenameWithExt2 = $file->getClientOriginalName();
            //Get just filename
            $filename = pathinfo( $filenameWithExt2, PATHINFO_FILENAME);
            //Get just extension
            $extension2 = $file->getClientOriginalExtension();
            //Filename to Store
            $fileNameToStore2 = $filename.'_'.time().'.'.$extension2;
            //Upload Image
            $path = $file->storeAs('public/paxsafety_folder',$fileNameToStore2);
            array_push($paxSafetyVideo, $fileNameToStore2);
        }


        $fileNameToStore = serialize($paxSafety);
        $fileNameToStore2 = serialize($paxSafetyVideo);
    }
    else
    {
        $paxSafety[]='noimage.jpg';
        $paxSafetyVideo[]='noimage.jpg';
    }


        foreach ($paxSafety as $key => $value) {
        $paxSafetyContent = new PaxSafety;
        $paxSafetyContent->paxsafety_image =  !empty($value) ? $value : '';
        foreach ($paxSafetyVideo as $key => $values) {
        $paxSafetyContent->paxsafety_video = !empty($values) ? $values : '';
        }

        $paxSafetyContent->save();
    }

2 个答案:

答案 0 :(得分:0)

  

模式PaxSafety中的更改设置为默认可为空。

 $table->string('paxsafety_video')->nullable()->change();

否则请更改控制器。

$paxSafetyContent = new PaxSafety;
$paxSafetyContent->paxsafety_image = !empty($values) ? $values : 'noimage.jpg';
$paxSafetyContent->paxsafety_video = NULL;
$paxSafetyContent->save();

一个或多个技巧是在$fillable模型中添加PaxSafety

protected $fillable = ['paxsafety_image','paxsafety_video'];

现在在控制器中的使用方式类似于使用create模型方法

PaxSafety::create(['paxsafety_image' => !empty($values) ? $values : 'noimage.jpg']);

答案 1 :(得分:0)

尝试一下。

$paxSafetyContent = new PaxSafety;

$paxSafetyContent->paxsafety_image = !empty($paxSafety) ? $paxSafetyVideo : '';

$paxSafetyContent->paxsafety_video = !empty($paxSafetyVideo) ? $paxSafetyVideo : ''; 

$paxSafetyContent->save();

在PaxSafety中添加以下内容:

protected $casts = [
    'paxsafety_image' => 'array',
    'paxsafety_video' => ' array'
];

还要确保两者都在模型的$ fillable区域中。

这将使您可以将图像和视频URL作为数组保存在数据库中。

希望这会有所帮助。