<?php

namespace ScoutElastic\Payloads;

use Exception;
use Illuminate\Database\Eloquent\Model;

class DocumentPayload extends TypePayload
{
    /**
     * @param Model $model
     * @throws Exception
     */
    public function __construct(Model $model)
    {
        if (!$model->getKey()) {
            throw new Exception(sprintf(
                'The key value must be set to construct a payload for the %s instance.',
                get_class($model)
            ));
        }

        parent::__construct($model);

        $this->payload['id'] = $model->getKey();
        $this->protectedKeys[] = 'id';
    }
}
<?php

namespace ScoutElastic\Payloads;

use Exception;
use Illuminate\Database\Eloquent\Model;

class DocumentPayload extends TypePayload
{
    /**
     * @param Model $model
     * @throws Exception
     */
    public function __construct(Model $model)
    {
        if ($model->getKey() === null) {
            throw new Exception(sprintf(
                'The key value must be set to construct a payload for the %s instance.',
                get_class($model)
            ));
        }

        parent::__construct($model);

        $this->payload['id'] = $model->getKey();
        $this->protectedKeys[] = 'id';
    }
}

Fix getKey check when ($model['id'] === 0)
Fix getKey check when ($model['id'] === 0)

I have inherited a project which has a row with an id of 0 which causes an exception to be thrown incorrectly.

Looking through $model->getKey() I noticed that it returns 'NULL' if a key doesn't exist but will return '(int) 0' if an 'id' key does exist with value of '(int) 0'.

'(int) 0' will evaluate as false even though it is in fact the correct value.