1

I use laravel and in my view I have an input datepicker like this :

{!! Form::text('DESMES_Date_Analyses',null ,['class' => 'form-control datepicker']) !!}

With this code :

$('.datepicker').datepicker({
        format: 'yyyy-mm-dd',
        todayHighlight: true,
        language : 'fr'
    });

And I get the value with something like this :

var date_analyses = $(this).find("input[name=DESMES_Date_Analyses]").val();

After that I insert in Database the value with Ajax request :

$.ajax({
            url: './register',
             headers: {'X-CSRF-TOKEN': token},
            data: {
                 DESMES_Date_Analyses : date_analyses
            },
            type: 'POST',
            datatype: 'JSON'
)};

And in my controller I do :

 public function register(Request $request) {
    if($request->ajax()) {
        Mesures::create($request->all());
    }
}

But if the value is empty, in my database I have 1900-01-01 and I just want NULL instead of this default date. How can I do this?

I tried to set date_analyses to null in javascript before sending it to my controller but it doesn't work... Thank you!

2
  • is 'date_analyses' really empty? Commented Feb 15, 2017 at 11:01
  • Yes and in case date_analyses is not empty, I tried setting it to null directly in javascript before sending it to my controller but I have the same result Commented Feb 15, 2017 at 11:19

4 Answers 4

1

I think it is your database table, which inserts 1900-01-01 while there is a null value passed for date_analyses. So check that once.

Sign up to request clarification or add additional context in comments.

3 Comments

When I try in SQL in my database I can to insert NULL for this column
What is the field type of that column in table? Does it have any default value assigned?
Also in controller, make that date_analyays is empty. Check like this dd($this->date_analysys)
1

First and foremost, you have to set nullable() on the table field you want to make nullable:

$table->date('DESMES_Date_Analyses')->nullable();

Some things to consider is that,

  • The field cannot be unique

After then (maybe), one important thing to do is to do some manual check on your DESMES_Date_Analyses param before saving it (and say you have other fields like name as well.).

.......
$data = [];
$data['name'] == $request->get('name');
if($date_analyses = $request->get('DESMES_Date_Analyses')) //i.e not null/empty
{
    $data['DESMES_Date_Analyses'] = date_analyses;        
}
Mesures::create($data); //so if date_analyses is not in the field it is saved as NULL

This means that if DESMES_Date_Analyses is not present then the query would exclude DESMES_Date_Analyses and set the field to NULL. Meaning that the query might look like this:

insert into the_table ('name') values ('some name'); //excluding the field `DESMES_Date_Analyses`

Comments

1

I found the solution : In my controller, just before the request to insert I put :

if($request->DESMES_Date_Analyses==""){$request->offsetSet('DESMES_Date_Analyses', NULL);}

Comments

1

You can pass NULL instate of empty value.

Note : column must be nullable.

public function register(Request $request) {

    $data = $request->input();

    if($data['DESMES_Date_Analyses'] == ''){
        $data['DESMES_Date_Analyses'] = NULL;
    }

    if($request->ajax()) {
        Mesures::create($data);
    }
}

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.