yabhq / laravel-scout-mysql-driver

Laravel Scout MySQL Driver
MIT License
522 stars 113 forks source link

Issue with indexing dates #54

Closed benjivm closed 1 year ago

benjivm commented 7 years ago

Hi, my model has two MySQL date columns, start_date and end_date stored as "Y-m-d", in addition to Laravel's created_at and updated_at.

In order for me to get them to display on the front end I need this line in my model:

protected $dates = ['start_date', 'end_date'];

I'm trying to integrate Laravel Scout with this driver but am running into an issue during index:

Creating index for App\Models\Report... 
In Carbon.php line 582:                 
  Data missing                          

Likewise, when trying to search the model I get this:

InvalidArgumentException
Data missing

The source of the problem seems to be these setters:

    /*
     * Convert start date to valid timestamp before adding to database.
     *
     * @param $value
     */
    public function setStartDateAttribute($value)
    {
        $this->attributes['start_date'] = Carbon::createFromFormat('m/d/Y', $value);
    }

    /**
     * Convert end date to valid timestamp before adding to database.
     *
     * @param $value
     */
    public function setEndDateAttribute($value)
    {
        $this->attributes['end_date'] = Carbon::createFromFormat('m/d/Y', $value);
    }

Which I use because the date picker data is flashed in the format m/d/Y and then converted on persistence.

How can I get around this?

msonowal commented 6 years ago

@gmask Can you Log the $value and output here

benjivm commented 6 years ago

What I don't get is why that mutator is even a concern of the indexer, since a mutator is only used on update of a new record?

Here is a tinker instance of the first record:

>>> $report = App\Models\Report::first();

=> App\Models\Report {#982 
    id: 1,
    author_id: 3, 
    customer_id: "555", 
    job_number: 123456, 
    contact_name: "Foo Bar", 
    contact_phone: "555-123-4567",
    start_date: "2017-11-06", 
    end_date: "2017-11-10", 
    active: 1,
    created_at: "2017-11-07 06:36:06",
    updated_at: "2017-12-12 10:08:21",
} 

>>> var_dump($report->created_at); 

object(Illuminate\Support\Carbon)#975 (3) {
    ["date"]=> 
    string(26) "2017-11-07 06:36:06.000000"
    ["timezone_type"]=>
    int(3) 
    ["timezone"]=> 
    string(19) "America/Los_Angeles" 
}

And here's how these records are created, $value is start_date and end_date before being converted to a Carbon timestamp.

>>> $report = new App\Models\Report;                             
=> App\Models\Report {#981}                                      
>>> $report->start_date = "12/13/2017";                          
=> "12/13/2017"                                                  
>>> $report->start_date;                                         
=> Illuminate\Support\Carbon @1513184688 {#975                   
     date: 2017-12-13 09:04:48.0 America/Los_Angeles (-08:00),   
   }                                                             
>>>