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

birdietorerik's avatar

Strange sql error

Hi!

Have this funtion in my model

 public static function getstartlistUsers($golfID,$dateselect,$userID)
    {

        $datofra = $dateselect.' 00:00:00';
        $datotil = $dateselect.' 23:59:59';
        
        $query = Startlist::where('golfclub_id', $golfID)
        ->select('startlists.*', 'users.name')
        ->where( 'startid', $starttid)
        ->whereBetween('created_at',[$datofra,$datotil])
        ->leftJoin('users', 'users.id', '=', 'startlists.user_id')
        ->get();

        return $startlist; 

    }
    

Bu this gives me this strange error ?

"message": "Unexpected data found. Unexpected data found. Data missing",
"exception": "Carbon\Exceptions\InvalidFormatException",
"file": "/home/vagrant/code/vendor/nesbot/carbon/src/Carbon/Traits/Creator.php",
"line": 676,
"trace": [
{
"file": "/home/vagrant/code/vendor/nesbot/carbon/src/Carbon/Traits/Creator.php",
"line": 699,
"function": "rawCreateFromFormat",
"class": "Carbon\Carbon",
"type": "::"
},
{
"file": "/home/vagrant/code/app/Models/Startlist.php",
"line": 89,
"function": "createFromFormat",
"class": "Carbon\Carbon",
"type": "::"
...
...
},

What is the issue here ?

Please help

0 likes
31 replies
Sinnbeck's avatar

Show the output of

dd($datofra, $datotil);
Sinnbeck's avatar

Oh and that is a php error. Nothing to do with sql

Tray2's avatar

That is not an SQL error, it's a Cabon error. My guess is that it tries to treat a null value as a date or some other unexpected value.

Snapey's avatar

Im guessing $dateselect is an instance of carbon

better;

    public static function getstartlistUsers($golfID,$dateselect,$userID)
    {
        return Startlist::where('golfclub_id', $golfID)
        ->select('startlists.*', 'users.name')
        ->where( 'startid', $starttid)
        ->whereDate('created_at', $dateselect)
        ->leftJoin('users', 'users.id', '=', 'startlists.user_id')
        ->get();

    }

but this won't work because you magic $startid from nowhere

Sinnbeck's avatar

Can you try (remember to import carbon at the start of the file)

$datofra = Carbon::parse($dateselect)->startOfDay();
$datotil = Carbon::parse($dateselect)->endOfDay();
birdietorerik's avatar

@Sinnbeck Sorry, same error

Output after using Carbon:....


Request: GET /api/v1/getstartlistUsers?status=5
2s ago
Carbon\Carbon @1656979200 {#2247 ▼
  #endOfTime: false
  #startOfTime: false
  #constructedObjectId: "000000006dad42ca00000000307a0993"
  #localMonthsOverflow: null
  #localYearsOverflow: null
  #localStrictModeEnabled: null
  #localHumanDiffOptions: null
  #localToStringFormat: null
  #localSerializer: null
  #localMacros: null
  #localGenericMacros: null
  #localFormatFunction: null
  #localTranslator: null
  #dumpProperties: array:3 [▶
    0 => "date"
    1 => "timezone_type"
    2 => "timezone"
  ]
  #dumpLocale: null
  #dumpDateProperties: null
  date: 2022-07-05 00:00:00.0 UTC (+00:00)
}
Request: GET /api/v1/getstartlistUsers?status=5
7s ago

Sinnbeck's avatar

@birdietorerik are you sure the error comes from this code? Or is there something you aren't showing us? How getstartlistUsers called?

birdietorerik's avatar

Hi All!

Tryed everything to fix this issue, but no luck

Have a new colum in database table -> startlists.regdate This colum only have the Date...

Still get the same error as before.

Here is my model:

<?php

namespace App\Models;

use \DateTimeInterface;
use App\Support\HasAdvancedFilter;
use Carbon\Carbon;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
use Illuminate\Support\Facades\DB;

class Startlist extends Model
{
    use HasAdvancedFilter;
    use SoftDeletes;
    use HasFactory;

    public $table = 'startlists';

    protected $dates = [
        'startid',
        'created_at',
        'updated_at',
        'deleted_at',
    ];

    protected $orderable = [
        'id',
        'match',
        'starthull',
        'startid',
        'hcp',
        'sh',
        'klasse',
        'status',
        'golfclub.name',
        'user.name',
        'tee.name',
        'typeofstartlist',
        'sloyfe.courcename',
        'nr_of_holes',
        'lasthole',
        'shotgun',
    ];

    protected $filterable = [
        'id',
        'match',
        'starthull',
        'startid',
        'hcp',
        'sh',
        'klasse',
        'status',
        'golfclub.name',
        'user.name',
        'tee.name',
        'typeofstartlist',
        'sloyfe.courcename',
        'nr_of_holes',
        'lasthole',
        'shotgun',
    ];

    protected $fillable = [
        'match',
        'starthull',
        'startid',
        'hcp',
        'sh',
        'klasse',
        'status',
        'golfclub_id',
        'user_id',
        'tee_id',
        'typeofstartlist',
        'sloyfe_id',
        'nr_of_holes',
        'lasthole',
        'shotgun',
        'created_at',
        'updated_at',
        'deleted_at',
    ];

    public function getStartidAttribute($value)
    {
        return $value ? Carbon::createFromFormat('Y-m-d H:i:s', $value)->format(config('project.time_format')) : null;
    }

    public function setStartidAttribute($value)
    {
        $this->attributes['startid'] = $value ? Carbon::createFromFormat(config('project.time_format'), $value)->format('H:i:s') : null;
    }

    public function golfclub()
    {
        return $this->belongsTo(Golfclub::class);
    }

    public function user()
    {
        return $this->belongsTo(User::class);
    }

    public function tee()
    {
        return $this->belongsTo(Tee::class);
    }

    public function sloyfe()
    {
        return $this->belongsTo(Courcegolfclub::class);
    }

    protected function serializeDate(DateTimeInterface $date)
    {
        return $date->format('Y-m-d H:i:s');
    }

    public static function getturneringsnavn()
    {
        return "TEST TURNERING";
    }


    public static function storestatustostartlist($status,$userID,$golfID,$datoen)
    {

        $datoen = date("Y-m-d");

        $query = Startlist::where('golfclub_id', $golfID)
            ->where('user_id', $userID)
            ->whereBetween('created_at',[$datoen,$datoen])
            ->update(['status' => $status]);
    
        return $query;
    
    }

    public static function getstartlists($userID,$golfID,$datoen)
    {
           
          $datoenfra = $datoen.' 00:00:00';
          $datoentil = $datoen.' 23:59:59';
          
          $query = Startlist::where('golfclub_id', $golfID)
          ->where('user_id', $userID)
          ->whereBetween('created_at',[$datoenfra,$datoentil])
          ->first();
          

          return  $query;

    }
    public static function getstartlistUsers($golfID,$dateselect,$userID)
    {
        // Finn start time user
        $startlist = Startlist::getstartlistForUser($golfID,$userID);

        if($startlist){
            $starttid = $startlist->startid;
        } else {
            $starttid = -1;
        }

        $dagensdato = date("Y-m-d");
        return Startlist::where('golfclub_id', $golfID)
        ->select('startlists.*')
        ->where( 'startid', $starttid)
        ->whereDate('regdate', '=', date('Y-m-d'))
        ->get();

        return $query;

    }
    
    public static function getstartlistForUser($golfID,$userID)
    {

        $datoen = date("Y-m-d");
        $startlist = Startlist::where('golfclub_id', $golfID)
        ->where('user_id', $userID)
        ->whereDate('regdate', '=',$datoen)
        ->first();

        return $startlist; 

    } 
    public static function getmatchnumber($golfclub_ID,$created_at,$startid)
    {

          // Check where player are
          $startlist = Startlist::where('golfclub_id', $golfclub_ID)
          ->where('startid', $startid)
          ->whereBetween('created_at',[$created_at,$created_at])
          ->first();
         

          if($startlist){
              $flightnumb =  $startlist->match;
          } else {
           

            $flightnumb2 = 0;

            $startlist = Startlist::where('golfclub_id', $golfclub_ID)
            ->whereBetween('created_at',[$created_at,$created_at])
            ->get();


            $flightnumb2 = $startlist->max('match');


            if($flightnumb2>0) {
                $flightnumb =  $flightnumb2+1;
            } else {
                $flightnumb = 1; 
            }

          }
          return  $flightnumb;

    }


    public static function storeStartlist($request,$userID)
    {
        
        $starthull = $request->startHole;
        $startid = $request->startTime;
        $startid = $startid.':00'; 
        $sloyfe_id =  $request->defcource;
        $utslagsted_id = $request->deftee;
        $shotgun = $request->shotgun;
        $nrOfHoles = $request->nrOfHoles;
        $golfID = $request->valggolfklubb;

        $timezone  = 2; //(GMT -5:00) EST (U.S. & Canada)
        $created_at = gmdate("Y-m-d", time() + 3600*($timezone+date("I")));
        
        $shotgunnr = 0;
        if($shotgun==="true"){
            $shotgunnr = 1;
        } 

        $nrOfHolesnr = 0;
        if($nrOfHoles==="true"){
            $nrOfHolesnr = 1;
        } 

        
        // Check if user exist
        //$statusPlayer =  Startlist::checkstatusstartlist($golfID,$userID,$created_at);

        $hcp = 0;
        $sh = 0;
        $klasse = "inegn";
        $firmanavn = "";


        $match = Startlist::getmatchnumber($golfID,$created_at,$startid);
        $starthull=$starthull;

        $startliste = new Startlist();
        $startliste->startid = $startid;
        $startliste->sloyfe_id = $sloyfe_id;
        $startliste->tee_id = $utslagsted_id;
        $startliste->match = $match;
        $startliste->user_id = $userID;
        $startliste->hcp = $hcp;
        $startliste->starthull = $starthull;
        $startliste->sh = $sh;
        $startliste->klasse = $klasse;
       // $startliste->created_at = $created_at;
        $startliste->status = 2;
        $startliste->nrOfHoles = $nrOfHolesnr; 
        $startliste->typeofstartlist = 1;
        $startliste->golfclub_id = $golfID;
        $startliste->lasthole = Startlist::getLastHole((int)$starthull,$shotgunnr,$nrOfHolesnr);  // MÅ Beregnes
        $startliste->shotgun = $shotgunnr;  // MÅ Beregnes
        $startliste->regdate = date("Y-m-d");
        $startliste->save();


        // $lagrerunde = Startlist::storeRoundUser();

        return  $startliste;


    }

    public static function checkstatusstartlist($userID,$golfID,$datoen)
    {

          /* 
          // Check where player are
          $startlist = DB::table('startlistes')
          ->select('startlistes.*')
          ->where('startlistes.golfclub_id', $golfID)
          ->where('startlistes.user_id', $userID)
          ->first();

          */

          $datofra = $datoen.' 00:00:00';
          $datotil = $datoen.' 23:59:59';

          $startlist = Startlist::where('golfclub_id', $golfID)
          ->where('user_id', $userID)
          ->whereBetween('created_at',[$datofra,$datotil])
          ->first();
          


        

          return  $startlist;

    }

   
    
    public static function getLastHole($starthull,$shotgun,$nrOfHolesnr)
    {
 
        $lasthole = 0;

        if(($starthull===1) and ($nrOfHolesnr===0)) {$lasthole=9;}
        if(($starthull===1) and ($nrOfHolesnr===1)) {$lasthole=18;}

        if(($starthull===10) and ($nrOfHolesnr===0)) {$lasthole=18;}
        if(($starthull===10) and ($nrOfHolesnr===1)) {$lasthole=9;}
      

        if($lasthole===0){
            if($shotgun===0)
            {
                if(($starthull===1) and ($nrOfHolesnr===0)) {$lasthole=9;}
                if(($starthull===1) and ($nrOfHolesnr===1)) {$lasthole=18;}
            } else {
                $lasthole = $starthull -1;
            }
             
        }

        return $lasthole;

    }

}
Snapey's avatar
Snapey
Best Answer
Level 122

Notice your code has changed quite a bit.

Only you know which line the error occurs on. (from the error)

if I was to guess, I would say this function is guilty

    public function getStartidAttribute($value)
    {
        return $value ? Carbon::createFromFormat('Y-m-d H:i:s', $value)->format(config('project.time_format')) : null;
    }

since you assume that $value is passed in the format 'Y-m-d H:i:s' yet you have already cast it to an instance of carbon in your dates array

birdietorerik's avatar

@Snapey Hi! When comment out getStartidAttribute..., the error is gone ? I have not created this function, and doent know where it is used ? Strange

birdietorerik's avatar

Hi All!

Snapey have spotted the error. It is Carbon error ? Have a colum in table that is Time, NOT Datetime

When i store time in database, the colum ->startid value is -> 07:07:00.000000 ????

This function :

public function getStartidAttribute($value)
    {
        return $value ? Carbon::createFromFormat('Y-m-d H:i:s', $value)->format(config('project.time_format')) : null;
    }

When i only use Time, then :

createFromFormat('Y-m-d H:i:s'......

Must be very wrong, tryed to change to -> createFromFormat('H:i:s' NO LUCK ???

birdietorerik's avatar

@frankielee Hi! This is a pain for me, ... :(

Change :

Carbon::createFromFormat('Y-m-d H:i:s....

To :

Carbon::createFromFormat('Y-m-d H:i:s.u.....   ????

My colum in database is of type Time only

Snapey's avatar

@birdietorerik then you cannot cast it to an instance of carbon since that infers a date. If you don't mind the date part being today then you can use createFromFormat but leave out the date part

Carbon::createFromFormat('H:i:s.u');
birdietorerik's avatar

@Snapey Hi! Sorry, but still not working.

I hope you can explane what i am missing here.

Get $startid from my vue (client side)

In my modell:

  public static function storeStartlist($request,$userID)
    {
        
        $starthull = $request->startHole;
        $startid = $request->startTime;
        //$startid = $startid; 
        $sloyfe_id =  $request->defcource;
        $utslagsted_id = $request->deftee;
        $shotgun = $request->shotgun;
        $nrOfHoles = $request->nrOfHoles;
        $golfID = $request->valggolfklubb;

        $timezone  = 2; //(GMT -5:00) EST (U.S. & Canada)
        $created_at = gmdate("Y-m-d", time() + 3600*($timezone+date("I")));
        
        $shotgunnr = 0;
        if($shotgun==="true"){
            $shotgunnr = 1;
        } 

        $nrOfHolesnr = 0;
        if($nrOfHoles==="true"){
            $nrOfHolesnr = 1;
        }

        $hcp = 0;
        $sh = 0;
        $klasse = "inegn";
        $firmanavn = "";


        $match = Startlist::getmatchnumber($golfID,$created_at,$startid);
        $starthull=$starthull;

        $startliste = new Startlist();
        $startliste->startid = $startid;      // Set Startid here
        $startliste->sloyfe_id = $sloyfe_id;
        $startliste->tee_id = $utslagsted_id;
        $startliste->match = $match;
        $startliste->user_id = $userID;
        $startliste->hcp = $hcp;
        $startliste->starthull = $starthull;
        $startliste->sh = $sh;
        $startliste->klasse = $klasse;
        $startliste->status = 2;
        $startliste->nrOfHoles = $nrOfHolesnr; 
        $startliste->typeofstartlist = 1;
        $startliste->golfclub_id = $golfID;
        $startliste->lasthole = Startlist::getLastHole((int)$starthull,$shotgunnr,$nrOfHolesnr);  // MÅ Beregnes
        $startliste->shotgun = $shotgunnr;  // MÅ Beregnes
        $startliste->regdate = date("Y-m-d");
        $startliste->save();


        // $lagrerunde = Startlist::storeRoundUser();

        return  $startliste;


    }

Change

public function getStartidAttribute($value)
    {
        return $value ? Carbon::createFromFormat('H:i:s', $value)->format(config('project.time_format')) : null;
    }

To:

public function getStartidAttribute($value)
    {
        return $value ? Carbon::createFromFormat('H:i:s.u', $value)->format(config('project.time_format')) : null;
    }

Still same problem, so what do i do wrong ???

Snapey's avatar

@birdietorerik I'm confused. Why is the field Startid but it contains a time?

What errors are you getting?

birdietorerik's avatar

@Snapey Hi! Startid contains only time, becource the table can have up to 4 people that i get from table. This function is where it get (when i fix the error)

Players have the same date + time. But maby can use datetime insted ?

  public static function getstartlistUsers($golfID,$dateselect,$userID)
    {
        // Finn start time user
        $startlist = Startlist::getstartlistForUser($golfID,$userID);

        if($startlist){
            $starttid = $startlist->startid;
        } else {
            $starttid = -1;
        }
       
       // Gets all players with same time and date
        $dagensdato = date("Y-m-d");
        return Startlist::where('golfclub_id', $golfID)
        ->select('startlists.*', 'users.name')
        ->where( 'startid', $starttid)
        ->whereDate('created_at', $dateselect)
        ->leftJoin('users', 'users.id', '=', 'startlists.user_id')
        ->get();


    }
Snapey's avatar

@birdietorerik

what I meant was why is it named startid and not starttime for instance

What errors are you getting?

Please or to participate in this conversation.