Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

muazzamazaz's avatar

Created at timestamp is null when retrieve

I am saving data using sensor where data is coming like this

$input = $request["date"].' '.$request["time"]; 

        $newDate= DateTime::createFromFormat('d/m/Y H:i:s',$input);     
$created_date = $newDate->format('Y-m-d H:i:s'); // for example
    try{
            
           $SensorData = SensorData::create([
            'sensor_id' => $request['sensor']['id'],            
            'created_at' => "".$created_date."",

          
        ]);

its saving datetime in database as 2020-05-04 01:10:12 but when retrieve value it shows null

0 likes
8 replies
m7vm7v's avatar

Can you confirm that when you visit manually the database you can see its been stored correctly?

What you can do is to cast the dates to the format you need.

On that note created_at is a field that will be automatically created when you execute SensorData::create(..);, you should be able to delete that entirely from the array.

If a specific date format is required to be the output then you can do

protected $casts = [
    'created_at' => 'datetime:Y-m-d',
];

which will make sure when you save the record will be with the asked format and when you take it from the DB it will be also in the format you want.

a4ashraf's avatar

@muazzamazaz

try this

try{
		
		$input = $request["date"].' '.$request["time"]; 
		
		$newDate= DateTime::createFromFormat('d/m/Y H:i:s',$input);     
		$created_date = $newDate->format('Y-m-d H:i:s'); // for example

		$SensorData = SensorData::create([
			'sensor_id' => $request['sensor']['id'],            
			'created_at' => $created_date,
		]);
	}
MichalOravec's avatar

@muazzamazaz So in database created_at is filled?

If so, could you post your select where you get data and also your model?

muazzamazaz's avatar
$sensor_data=SensorData::join('sensors','sensors.id','=','sensor_id')->orderBy('sensor_data.created_at','DESC')->get();

Models

class Sensor extends Model
{

    protected $fillable = [
        'name',
        'type',
        'machine_id',
    ];

public function sensor() {
		return $this->hasMany('App\SensorData');
	}
}

class SensorData extends Model
{

 protected $table = 'sensor_data';
public $timestamps = false;
     protected $fillable = [
        'sound',
        'proximity',
        'light',
        'humidity',
        'temprature',
        'sensor_id',
        'created_at',        
    ];

public function sensors() {
		return $this->belongsTo('App\Sensor');
	}

}
muazzamazaz's avatar

Using DB clause has solved the problem

$sensor_data = DB::table('sensors')
            ->join('sensor_data', 'sensors.id', '=', 'sensor_data.sensor_id')
            ->get();
Snapey's avatar

if both tables have the same created_at fields then only one can win. You need to select the field you want or alias one to a different name.

Its always problematic joining tables that have the same column names.

Please or to participate in this conversation.