I'm trying to update the date field if it is Null or if it is not null then put the null in table. but problem is date is stored but null value is not stored. can anyone solve this problem?
#This is controller method
public function taskcompleted(Request $request, $id)
{
$task = Todo::findOrFail($id);
// if ($task->completed_at == Null) {
// $task->completed_at = Carbon::now();
// }
// else {
// $task->completed_at = 'Null';
// }
($task->completed_at == Null) ? $task->completed_at = Carbon::now() : $task->completed_at = 'Null';
$task->save();
return back();
}
#this is Model
<?php
namespace App;
use Carbon\Carbon;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
class Todo extends Model
{
use SoftDeletes;
//Table Name
protected $table = 'todos';
//Primary key
public $primaryKey = 'id';
protected $fillable = [
'todoable_id',
'todoable_type',
'title',
'description',
'user_id',
'priority',
'outcome_id',
'tasktype_id',
'started_at',
'due_time',
'completed_at',
];
protected $casts = [
'started_at' => 'datetime',
'due_time' => 'datetime',
'completed_at',
];
protected $dates = [
'started_at',
'due_time',
'completed_at',
'created_at',
'updated_at',
'deleted_at',
];
public function setDueTimeAttribute($date)
{
// $attributes['due_time'] = Carbon::createFromFormat('d-m-Y H:i:s', $date);
$this->attributes['due_time'] = Carbon::parse($date)->format('Y-m-d H:i:s');
}
public function setStartedAtAttribute($date)
{
$this->attributes['started_at'] = Carbon::parse($date)->format('Y-m-d H:i:s');
}
public function setCompletedAtAttribute($date)
{
$this->attributes['completed_at'] = Carbon::parse($date)->format('Y-m-d H:i:s');
}