Reputation: 133
I work on Laravel 5.2 and I have a form submitted in Ajax to insert data in a table. This is my function in the controller :
public function saveChange(Request $request) {
if($request->ajax()) {
Affectationabc::create($request->all());
return response()->json([
'testdata' => $request->all()
]);
}
}
And I want to add at the same time the first day of current year and the last day in 2 others columns.
I tried to do something like this :
public function saveChange(Request $request) {
if($request->ajax()) {
$firstDay = date('l',strtotime(date('Y-01-01')));
$lastDay = date('l',strtotime(date('Y-12-31')));
$request->merge(array('HISTO_AFFECT_DATE_DEB' => $firstDay, 'HISTO_AFFECT_DATE_FIN' => $lastDay));
Affectationabc::create($request->all());
return response()->json([
'testdata' => $request->all()
]);
}
}
But It doesn't works, probably because of the format of the date. My columns HISTO_AFFECT_DATE_DEB and HISTO_AFFECT_DATE_FIN are dates in format Y-m-d.
How can I do this please ?
EDIT:
Maybe there is a problem in my Model, this is my code :
use Illuminate\Database\Eloquent\Model;
class Affectationabc extends Model
{
protected $table = "Histo_AFFECT_ABC";
protected $primaryKey = 'HISTO_AFFECT_ID';
const CREATED_AT = 'HISTO_AFFECT_DATE_CREA';
const UPDATED_AT = 'HISTO_AFFECT_DATE_MAJ';
protected $dates = ['HISTO_AFFECT_DATE_CREA','HISTO_AFFECT_DATE_MAJ','HISTO_AFFECT_DATE_DEB','HISTO_AFFECT_DATE_FIN'];
protected function getDateFormat()
{
return 'Y-m-d';
}
protected $fillable = [
'HISTO_AFFECT_ID_CATEG',
'HISTO_AFFECT_ID_PERSONNEL',
'HISTO_AFFECT_DATE_DEB',
'HISTO_AFFECT_DATE_FIN',
'HISTO_AFFECT_COMMENTAIRES',
'HISTO_AFFECT_DATE_CREA',
'HISTO_AFFECT_DATE_MAJ'
];
public function setHistoAffectDateDebAttribute($value)
{
$this->attributes['HISTO_AFFECT_DATE_DEB'] = \Carbon\Carbon::createFromFormat('d/m/Y', $value)->format('Y-m-d') ;
}
public function setHistoAffectDateFinAttribute($value)
{
$this->attributes['HISTO_AFFECT_DATE_FIN'] = \Carbon\Carbon::createFromFormat('d/m/Y', $value)->format('Y-m-d') ;
}
}
EDIT2 : It seems I have an error 500 when I try to add this dates !
Upvotes: 1
Views: 1351
Reputation: 133
I had this 2 functions in my Model that created conflicts :
public function setHistoAffectDateDebAttribute($value)
{
$this->attributes['HISTO_AFFECT_DATE_DEB'] = \Carbon\Carbon::createFromFormat('d/m/Y', $value)->format('Y-m-d') ;
}
public function setHistoAffectDateFinAttribute($value)
{
$this->attributes['HISTO_AFFECT_DATE_FIN'] = \Carbon\Carbon::createFromFormat('d/m/Y', $value)->format('Y-m-d') ;
}
I deleted them and I did this in my controller :
public function saveChange(Request $request) {
if($request->ajax()) {
$firstDay = Carbon::now()->startOfYear()->format('Y-m-d');
$lastDay = Carbon::now()->endOfYear()->format('Y-m-d');
$request->merge(array('HISTO_AFFECT_DATE_DEB' => $firstDay, 'HISTO_AFFECT_DATE_FIN' => $lastDay));
Affectationabc::create($request->all());
return response()->json([
'testdata' => dd($request->all())
]);
}
}
And everything works perfectly! I found the error in logs, thanks all !
Upvotes: 0
Reputation: 163978
You didn't tell anything about what error do you get, but if it's date related, try to use Carbon:
$firstDay = Carbon::now()->startOfYear()->format('Y-m-d');
$lastDay = Carbon::now()->endOfYear()->format('Y-m-d');
Upvotes: 1