I am getting errors on submitting a form to the server that has a bootstrap-datepicker field. What should I do now?
The error report is:
InvalidArgumentException in Carbon.php line 425:
Unexpected data found.
Unexpected data found.
Data missing
in database migration file:
$table->timestamp('dob'); // dob is date of birth
in my model:
protected $dates = ['dob'];
HTML:
<div class="form-group {{ $errors->has('dob') ? 'has-error' : '' }}">
<label class="col-sm-2 control-label">Birthday*</label>
<div class="col-sm-10">
<input type="text" class="form-control" name="dob" required="" id="datepicker" value="{{ old("dob") }}">
{!! $errors->first('dob','<span class="help-block">:message</span>') !!}
</div>
</div>
script:
<script src="{{ url('js/bootstrap-datepicker.js') }}" charset="utf-8"></script>
<script>
$('#datepicker').datepicker({
autoclose: true
});
</script>
If you're trying to get the bootstrap-datepicker jQuery plugin to work with Laravel date formats, here are the steps:
Step 1, set this format in your JavaScript:
jQuery('.datepicker').datepicker({
format: 'yyyy-mm-dd'
});
Step 2, add the time part of the date when saving the date in your Controller, example:
$model->start_date = $request->start_date .' 00:00:00';
The datpicker format is different from timestamp format so you need to follow one of the solutions:
1.change the accepted datepicker's format using format option
2.follow the following link to know how to change the timestamp's format to whatever you want or even make it nullable.
Laravel "Unexpected data found" error when trying to change format of Carbon created_at date
You need to change your field before saving in DD,
In order to do that add something like this function to your relevant model,
Notice that you must follow the naming convention which is //setNameAttribute
. Then pass the date taken from datepicker
public function setDob($date)
{
$this->attributes['dob']=Carbon::createFromFormat('Y-m-d H:i:s',$date);
}
Related
im a Laravel newbie and i try do generate Customerid for my Customer.Create.
My idea is to use they IdGenerator to generate id and then set the Id automatic in disabled input field on my creat.blade.php.
But im not shure is this idea right.
i write this in my web.php:
use Haruncpi\LaravelIdGenerator\IdGenerator;
Route::get('/customer/create', function(){
$config = ['table' => 'customers', 'length' => 10, 'prefix' => 'K-'];
$id = IdGenerator::generate($config);
return $id;
});
and this is the html snippen from my create.blade.php
<div class="col-auto">
<label class="sr-only" for="inlineFormInputGroup">Firma</label>
<div class="input-group mb-2">
<div class="input-group-prepend">
<div class="input-group-text"><i class="fas fa-building"></i></div>
</div>
<input type="text" class="form-control" id="inlineFormInputGroup" placeholder="{{ $id }}" disabled>
</div>
</div>
but i get everytime this erros message:
Exception table field type is bigint but prefix is string
Can you help me, is this the right way or do you have a better idea?
greetz Marcel
Change the field type of prefix to char and in your /database/migrations/your_table.php
change $table->bigInt('prefix') to $table->char('prefix') and then rerun your migrations Make sure you do not delete important data from your database please check the Laravel docs if you are not sure what you're doing.
I am using bootstrap-datepicker with vue js. The problem is, when I select date it comes to the input field but whenever I shift to another input field, the date vanishes. how do I make the date stay even if I switch to another field. below is my code
$('.datepicker').datepicker();
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-datepicker/1.9.0/js/bootstrap-datepicker.min.js" integrity="sha256-bqVeqGdJ7h/lYPq6xrPv/YGzMEb6dNxlfiTUHSgRCp8=" crossorigin="anonymous"></script>
<div class="input-group">
<div class="input-group-prepend">
<span v-bind:style="{ width: prependWidth + 'px' }" class="input-group-text">Acceptance Date</span>
</div>
<input data-provide="datepicker" v-model="formData.acceptance_date" id="acceptance_date" name="acceptance_date" type="text" placeholder="" class="form-control">
</div>
here is my vue data
formData: {
event_name: '',
ba_alt: '',
bn_alt: '',
acceptance_date: ''
},
Want to change date of birth format to dd/mm/yyy in laravel .
Actually i want it to be saved in dd/mm/yyyy format but it should be send to the database in yyyy/mm/dd format . How can it be done ?? . Here is the code
<div class="col-md-4">
<div class="form-group">
<label>Date of Birth:</label>
<span class="data_fields data_personal">{{ date_to_indian($cust_data->dob) }}</span>
<span class="edit_fields edit_personal"><input type="text" class="form-control" value="{{ $cust_data->dob }}" name="dob"></span>
</div>
</div>
Use Carbon's format() method:
Carbon::parse($date)->format('d/m/Y');
If date is in Eloquent $dates property, just use format():
$date->format('d/m/Y')
You can use laravel accessor mutators in laravel. Put below code in your model
protected $dates = [
'dob',
];
//This method automatically save date Y-m-d format in database
public function setDobAttribute($date)
{
$this->attributes['dob'] = Carbon::createFromFormat('d-m-Y', $date)->format('Y-m-d');
}
//This method automatically fetch date d-m-Y format from database
public function getDobAttribute($date)
{
return Carbon::createFromFormat('Y-m-d', $date)->format('d-m-Y');
}
You must be use carbon class namespace as following:
use Carbon;
Try this code:
<span class="edit_fields edit_personal"><input type="text" class="form-control" value="<?php echo date("d/m/Y",strtotime($cust_data->dob )); ?>" name="dob"></span>
You can use Date Mutators to convert a date to instances of Carbon, which extends the PHP DateTime class to provide an assortment of helpful methods.
Add the following property in your model:
protected $dates = [
'dob',
];
Then use it in your view as:
{{ $cust_data->dob->format('d/m/Y') }}
When retrieving attributes that are listed in your $dates property, they will automatically be cast to Carbon instances, allowing you to use any of Carbon's methods on your attributes
Use carbon date format method format()
{{ Carbon\Carbon::parse($cust_data->dob)->format('d-m-Y') }}
OR
you can use PHP date function date() for it:
{{date('d-m-Y', strtotime($cust_data->dob))}}
Both are return same result but I recommend you to use Carbon because Carbon is inherited from PHP DateTime class.
I think you can try twig filters here {{ $cust_data->dob | date('d-m-Y') }}
<div class="col-md-4">
<div class="form-group">
<label>Date of Birth:</label>
<span class="data_fields data_personal">{{ $cust_data->dob | date('d-m-Y') }}</span>
<span class="edit_fields edit_personal"><input type="text" class="form-control" value="{{ $cust_data->dob }}" name="dob"></span>
</div>
</div>
You can use $cust_data->dob->format('d/m/Y');
for more options http://carbon.nesbot.com/docs/
I'm working with Laravel 5, in a view I'm using the bootstrap datetimepicker I have a timestamp saved in the database, and when the user load that view I want to preload that date in the datetimepicker. As I read there's a method datepicker('setDate',startDate); to set the date, but I need to assign the value that is accesible trought PHP, as I am using blade my timestamp is accesible with:
{{$contract->myTimestamp}}
So, I can I do it?
HTML Code:
<div class="col-sm-10" style="height:10px;">
<div class="form-group">
<div class='input-group date' data-date-startdate="{{$contract->myTimestamp}}" name="datepicker" data-date-format="yyyy-mm-dd hh:mm:ss" id='datetimepicker{{$contract->id}}' >
<input type='text' class="form-control" id="fecha">
<span class="input-group-addon">
<span class="glyphicon glyphicon-calendar">
</span>
</span>
</div>
</div>
in the JS I added:
$(document).ready(function(){
$(this).datetimepicker().datetimepicker('setDate',startDate);
});
Now It shows me the date, but doesn't let me edit it, I mean when I click on the button to chage the date nothing happens,So I whas thinking that it could be a trouble with the format of the date, because in the database I have something like 2015-11-28 20:10:14 and in the datetimepicker I have 12/03/2015 12:00 AM and they are differents formats. So how can I transform the date?thanks!
I write the form.blade.php like this:
<div class="form-group">
<label>Start at</label>
<div id="start_date" class="input-group date form_datetime col-md-4" data-date-format="yyyy-mm-dd hh:ii:ss">
<input class="form-control" name="start_at" size="16" type="text" value="{{ $activity->start_at }}">
<span class="input-group-addon"><span class="glyphicon glyphicon-remove"></span></span>
<span class="input-group-addon"><span class="glyphicon glyphicon-th"></span></span>
</div>
</div>
Both Create and Edit method use the view to display the form. In the Edit method, the $activity is used and everything is normal. But in the Create method, the $activity is null, so I have a error in {{ $activity->start_at }}.
I know I can use if(!empty($activity)) to prevent this error. but I do not want to use this anywhere.
What is the better way?
You could use this,
{{ $activity->start_at or '' }}
I also like to reuse code and use the same view to create or edit an object. What I do is to pass a brand new instance of the object to the create view. This way I have the object on its initial state including defaults (if any) and I am able to prefill those defaults (if any) on the displayed form. It gives me an additional benefit: if anything goes wrong with the validation at the server level the user doesn't lose any data, I just need to do something like this in the view:
<div class="form-group">
<label for="test_field" class="control-label">Fecha de firma</label>
<input type="text" value="{{ ( !empty($isabi) ? $isabi->fecha_firma : old('fecha_firma')) }}"
name="fecha_firma" id="isabi-fechafirma" class="form-control" placeholder="Este campo es obligatorio">
</div>
This is the create method with extra functionality removed for clarity (check if the user is authenticated, linking the created object with the company the user works for and other stuff):
public function create()
{
return view('isabis.create', [
"isabi" => new Isabi()
])->with($this->data);
}
I use $this->data for view configuration. Hope this helps.