0

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

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

EDIT

<?php

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

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')) {
        foreach ($request->file('awards_image') as $key => $file) {
            $filenameWithExt = $file->getClientOriginalName();
            $filename = pathinfo($filenameWithExt, PATHINFO_FILENAME);
            $extension = $file->getClientOriginalExtension();
            $fileNameToStore = $filename . '_' . time() . '.' . $extension;
            $path = $file->storeAs('public/awards_images', $fileNameToStore);
            array_push($awards, $fileNameToStore);
        }
        $fileNameToStore = serialize($awards);
    } else {
        $fileNameToStore = 'noimage.jpg';
    }

    $awardsContent = new Award;
    $awardsContent->title = $request->input('title');
    $awardsContent->description = $request->input('description');
    $awardsContent->awards_image = $fileNameToStore;
    $awardsContent->save();

    return redirect('/admin/airlineplus/awards')->with('success', 'Content Created');
}
1
  • 1
    allow your column to accept null values, because your code could allow null values to be passed which contradicts with Column conditions. Commented Nov 28, 2018 at 2:35

2 Answers 2

1

Your database doesn't currently allow the title column to be null. You can either change that field in the table to be nullable, or you can update your code to use an empty string if the value from the request is null.

$awardsContent->title = $request->filled('title') ? $request->get('title') : '';
Sign up to request clarification or add additional context in comments.

2 Comments

Sir, can you answer my EDIT code above your answer did not work when I foreach loop the code sorry please answer help me thanks
You should be able to do the same kind of thing for that situation. Something like this should work. $awardsContent->title = !empty($request->title[$key]) ? $request->title[$key] : '';
0

You have to change your database migration schema like this

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

and then refresh your migration with this,

php artisan migrate:refresh

5 Comments

@SummerWinter sure
bro all of my data in database is gone when i refresh :(
In order to make changes you need to refresh database. Im sorry I don't know you are not familiar with above command
that's okay sir can you please answer my edited code above it seems like the other answer did not work
refreshing the database is not a good suggestion. in earlier versions of laravel, you should use the sql 'ALTER TABLE' syntax. in later versions, use the ->change() method.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.