0

Hello I would like to insert a null value to my database but i have no clue how to do it is giving an error

How to insert an empty value to input text and set it to null to database, as I do this Im having an error because the rows of the database does not have a value. how can I do this without an error.

here is my error

As you can see I have a foreach loop so inserting 1 input text value returns nothing on the database.

SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'title' cannot be null (SQL: insert into awards (title, description, awards_image, updated_at, created_at) values (, , a:0:{}, 2018-11-28 10:29:35, 2018-11-28 10:29:35))

my Controller

public function store(Request $request)
{
    $this->validate($request, [
        'title' => 'nullable',
        'description' => 'nullable',
        'awards_image.*' =>  'image|nullable|max:1999'
    ]);
    $awards = [];
    if ($request->has('awards_image'))
    {   
        //Handle File Upload


        foreach ($request->file('awards_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/awards_images',$fileNameToStore);
            array_push($awards, $fileNameToStore);
        }

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


foreach ($awards as $key => $values) {
            $awardsContent = new Award;
            $awardsContent->title = $request->title[$key];
            $awardsContent->description = $request->description[$key];
            $awardsContent->awards_image = $values;
            $awardsContent->save();
            }

}
  • 1
    Make your database table nullable as i've explained in previous question. – Sagar Gautam Nov 28 '18 at 05:09
  • if attribute has no value put empty string like `' '` it's allows to null. – Gabrielle-M Nov 28 '18 at 05:27
  • @Gabrielle how can I do that? please help me with that syntax thns –  Nov 28 '18 at 05:28
  • @SagarGautam can i do that without `php artisan migrate:refresh` ? –  Nov 28 '18 at 05:29
  • @Gabrielle, the empty string and `null` are two very different things. – Greg Schmidt Nov 28 '18 at 05:35
  • @Gabrielle i like your logic sir, but how can i do that in the foreach loop section can you kindly help me please thanks –  Nov 28 '18 at 05:35
  • @Gabrielle ok can we go to the string ` '' ` ?how can we do that? –  Nov 28 '18 at 05:36
  • 2
    Possible duplicate of [Laravel - Inserting empty to Input Form and Insert Null Value to Database](https://stackoverflow.com/questions/53511239/laravel-inserting-empty-to-input-form-and-insert-null-value-to-database) – Greg Schmidt Nov 28 '18 at 05:41

2 Answers2

3

If you do not want to change the table then try this:

foreach ($awards as $key => $values) {
        $awardsContent = new Award;
        $awardsContent->title = !empty($request->title[$key]) ? $request->title[$key] : '';
        $awardsContent->description = $request->description[$key];
        $awardsContent->awards_image = $values;
        $awardsContent->save();
        }

}

Some more info in empty():

http://php.net/manual/en/function.empty.php

If you are happy to change the table the create a migration where this is in up

Schema::table('awards', function(Blueprint $table) {

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

});

of course you will have to change this to what you have already but the important part is ->nullable()->change(); the rest of the line should be the same as your initial migration.

Some more info on migration changes:

https://laravel.com/docs/5.7/migrations#modifying-columns

I hope this helps

Josh
  • 1,316
  • 10
  • 26
  • Thanks @Josh this is a lot of info. I hope you will reply if ever i need helps on this code thank you very much –  Nov 28 '18 at 05:42
  • how about the Image @Josh ? how can I make that null too? –  Nov 28 '18 at 05:43
  • you can use the same for the awards_image as the title just change the code to match the awards_image – Josh Nov 28 '18 at 05:46
  • I mean it is $fileNameStorage .. i think it will be different in approach dont ya think sir? –  Nov 28 '18 at 05:47
  • last problem is when I insert `Title` and `Description` and leave Input File null , i cant insert any data why is that? –  Nov 28 '18 at 05:56
  • You are using the `$awards` variable for foreach but you are not setting anything to it. try changing `$fileNameToStore='noimage.jpg';` to `$awards[] = 'noimage.jpg';` – Josh Nov 28 '18 at 06:00
  • it does not work sir, why is that .. im having this last issue with this foreach loop :( I also need to insert title and description and LEAVE input file alone. but as im doing that .. it does not insert any data –  Nov 28 '18 at 06:05
  • Wait sir it works ! but the title is the same value as description –  Nov 28 '18 at 06:06
  • sit it works but lastly the `TITLE ` value is the same as `DESCRIPTION ` value .. like for example, I insert **dummy 1** in the title then the description will also be ""dummy 1** . –  Nov 28 '18 at 06:09
  • try making it 'test title' and 'test description' Also if you are adding multiple with not image it will not work – Josh Nov 28 '18 at 06:12
  • 1
    HAHA it works sir time wasted just a typo error THANK YOU you saved my time today –  Nov 28 '18 at 06:13
  • sir how can I make this image null or no value `$paxSafetyContent->paxsafety_image = !empty($value) ? $value : '';` is this code correct? –  Nov 28 '18 at 07:24
  • Yeah if the value is empty it will be `''` – Josh Nov 28 '18 at 07:35
  • sir please refer to this [Link for: Image Null Problem](https://stackoverflow.com/questions/53514339/laravel-inserting-empty-string-or-null-images-to-the-database) for you to help me more –  Nov 28 '18 at 07:36
0

Make a new migration

php artisan make:migration fix_title_in_table_awards --table=awards

Then in the migration created first drop the column, and then recreate the column making it nullable..

Schema::table('awards', function (Blueprint $table) {
    $table->dropColum('title');
});

Schema::table('awards', function (Blueprint $table) {
    $table->string('title')->nullable();
});

Take care of this action because you will lost all the data in that column...maybe you can copy the actual values of title to another column untill you make the change...

Walter Cejas
  • 1,924
  • 1
  • 12
  • 22