Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

"Expected MatanYadaev\EloquentSpatial\GeometryCast, string given" while swapping two records #79

Closed
RahulKRaj7 opened this issue Feb 7, 2023 · 4 comments · Fixed by #81

Comments

@RahulKRaj7
Copy link

Hi!,
i am trying to implement draft functionality on model. in that while trying to swap two records. i am facing Expected MatanYadaev\EloquentSpatial\GeometryCast, string given this type of error.
Updating location data doesn't work.
Below is the snapshot of swapping code.

$oldAttributes = $published?->getAttributes() ?? [];
        $newAttributes = $this->getAttributes();
        Arr::forget($oldAttributes, $this->getKeyName());
        Arr::forget($newAttributes, $this->getKeyName());

        $published->forceFill($newAttributes);
        $this->forceFill($oldAttributes);

The field is casted in model as
location' => Point::class,

in the migration the field is created as
$table->point('location')->nullable();

Using Laravel-Drafts package for implementing draft functionality.

@RahulKRaj7 RahulKRaj7 changed the title Facing "Expected MatanYadaev\EloquentSpatial\GeometryCast, string given" while swapping two records "Expected MatanYadaev\EloquentSpatial\GeometryCast, string given" while swapping two records Feb 7, 2023
@MatanYadaev
Copy link
Owner

Hi @RahulKRaj7, can you share with me a wider context? Ideally, if you could fork this repo and add a failing test, it will help me to understand the issue better.

I reproduced this error with a slightly different error message: InvalidArgumentException : Expected MatanYadaev\EloquentSpatial\GeometryCast, Illuminate\Database\Query\Expression given.

So I would like to understand how to reproduce your case.

@RahulKRaj7
Copy link
Author

RahulKRaj7 commented Feb 16, 2023

Thanks @MatanYadaev for your reply and for your work on this library. but sorry i didn't get time to fork this repo and add and test the failing test. Here's a failing test, which I thought would pass:

it('throws exception when swapping geometry field in two records using getAttributes()', function (): void
{
	expect(function (): void {
		$point = new Point(0, 180);
		$point2 = new Point(0, 0);
		$testPlace = TestPlace::factory()->make([
			'point' => $point,
		]);
		$testPlace2 = TestPlace::factory()->make([
			'point' => $point2,
		]);
		$oldAttributes = $testPlace->getAttributes();
		$newAttributes = $testPlace2->getAttributes();
		
		Arr::forget($oldAttributes, $testPlace->getKeyName());
		Arr::forget($newAttributes, $testPlace->getKeyName());
		
		$testPlace->forceFill($newAttributes);
		$testPlace2->forceFill($oldAttributes);
	})->toThrow(InvalidArgumentException::class);
});

@MatanYadaev
Copy link
Owner

@RahulKRaj7 Fixed, thanks for the help.

@hiteshsamcom
Copy link

@MatanYadaev faces this issue in the v.2.10 branch so can you please update it?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants