Skip to content

Custom Casts to allow RAW Values #47395

Closed Answered by monkeyphysics
ojsoft asked this question in Ideas
Discussion options

You must be logged in to vote

Does this not work?

App\Casts\Point.php

<?php

namespace App\Casts;

use Illuminate\Support\Facades\DB;
use Illuminate\Contracts\Database\Eloquent\CastsAttributes;

class Point implements CastsAttributes
{
    public function get($model, $key, $value, $attributes)
    {
        // https://stackoverflow.com/questions/37467050/convert-mysqls-point-to-text-in-php
        return unpack('x4/corder/Ltype/dlng/dlat', $value);
    }

    public function set($model, $key, $value, $attributes)
    {
        return DB::raw(sprintf('POINT(%f, %f)', $value['lng'], $value['lat']));
    }
}

Replies: 2 comments 5 replies

Comment options

You must be logged in to vote
2 replies
@Cepairda
Comment options

@monkeyphysics
Comment options

Answer selected by ojsoft
Comment options

You must be logged in to vote
3 replies
@ojsoft
Comment options

@monkeyphysics
Comment options

@monkeyphysics
Comment options

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Ideas
Labels
None yet
3 participants