0

I can't insert a string or a null value in the database resulting in the following error:

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))

Controller

     $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();
    }
  • The $paxSafetyContent->paxsafety_video = 'some-value' is not setting the value, make sure your model fillable has the field name :paxsafety_video. And make the paxsafety_video null in the database if you need to set null values, – Shan Nov 28 '18 at 07:50

2 Answers2

0

change in schema PaxSafety set default nullable.

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

otherwise CHANGE IN CONTROLLER..

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

one or more tricks is add $fillable columns in PaxSafety Model

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

now in controller used like that using create model method

PaxSafety::create(['paxsafety_image' => !empty($values) ? $values : 'noimage.jpg']);
Jignesh Joisar
  • 13,720
  • 5
  • 57
  • 57
  • is there a way that i cant delete my data in my database. if I meddled my tables maybe my data will be removed –  Nov 28 '18 at 07:56
  • Error still persists :( can u edit my code pls? I 've also added the fillable u suggests on the last trick but the error still persists :( help me sir pls –  Nov 28 '18 at 08:02
0

Try this.

$paxSafetyContent = new PaxSafety;

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

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

$paxSafetyContent->save();

In the PaxSafety add the following:

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

Also make sure both are in the $fillable ariae in the model.

This will allow you to save the image and video URLs as an array in the DB.

Hope this helps.

Josh
  • 1,316
  • 10
  • 26