mardi 27 novembre 2018

Laravel - Inserting Empty String or null Images to the Database

My problem is I cant insert a string or null value in the database and it gives me an error

My Error is

SQLSTATE[HY000]: General error: 1364 Field 'paxsafety_video' doesn't have a default value (SQL: insert into pax_safeties (paxsafety_image, updated_at, created_at) values (Desert_1543390457.jpg, 2018-11-28 15:34:17, 2018-11-28 15:34:17))

my Code in Controller

public function store(Request $request)
{
    $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'))
    {   
        //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);
        }
        $fileNameToStore = serialize($paxSafety);
    }else{
        $paxSafety[]='noimage.jpg';
    }

    if( $request->has('paxsafety_video')) {
        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);
        }       
        $fileNameToStore2 = serialize($paxSafetyVideo);
    }
    else
    {
        $paxSafetyVideo[]='noimage.jpg';
    }


        foreach ($paxSafety as $key => $value) {
        $paxSafetyContent = new PaxSafety;
        $paxSafetyContent->paxsafety_image =  !empty($value) ? $value : 'noimage.jpg';
        $paxSafetyContent->save();

        foreach ($paxSafetyVideo as $key => $values) {
        $paxSafetyContent = new PaxSafety;
        $paxSafetyContent->paxsafety_video = !empty($values) ? $values : 'noimage.jpg';
        $paxSafetyContent->save();
        }

    }
    return redirect('/admin/airlineplus/paxsafety')->with('success', 'Inflight Magazine Content Inserted');
}



from Newest questions tagged laravel-5 - Stack Overflow https://ift.tt/2SfPxjk
via IFTTT

Aucun commentaire:

Enregistrer un commentaire