0

I have a column that was added to a Eloquent Model using a raw SQL query, but I am unable to get the value back form the model when doing get(). I can print the model out using print_r() and see the value and column added into the [attributes:protected] and [original:protected] fields, but it doesn't return the value when I do get(). I added an appends attribute and accessor method but it returns null. Ex:

$query = $query->select('*', Capsule::raw('(3959 * acos(
                                                         ( cos( radians( ' . $zip['latitude'] . ' ) )
                                                           * cos( radians( latitude ) )
                                                           * cos( radians( longitude ) - radians( ' . $zip['longitude'] . ' ) ))
                                                         + ( sin( radians( ' . $zip['latitude'] . ' ) )
                                                             * sin( radians( latitude ) ))
                                                   )
                                       ) AS distance')
                                   )->orderBy('distance');

Object:

                    [attributes:protected] => Array
                        (
                            [zip_id] => 11753
                            [zipcode] => 29210
                            [territory_id] => 41
                            [latitude] => 34.04814020
                            [longitude] => -81.10814000
                            [last_verified] => 0000-00-00 00:00:00
                            [nextCheck] => 2020-03-16 17:40:50
                            [active] => 1
                            [distance] => 5.8993697166443E-5
                        )

                    [original:protected] => Array
                        (
                            [zip_id] => 11753
                            [zipcode] => 29210
                            [territory_id] => 41
                            [latitude] => 34.04814020
                            [longitude] => -81.10814000
                            [last_verified] => 0000-00-00 00:00:00
                            [nextCheck] => 2020-03-16 17:40:50
                            [active] => 1
                            [distance] => 5.8993697166443E-5
                        )

Returns:

    {
        "zip_id": 11753,
        "zipcode": "29210",
        "territory_id": 41,
        "latitude": "34.04814020",
        "longitude": "-81.10814000",
        "last_verified": "0000-00-00 00:00:00",
        "nextCheck": "2020-03-16 17:40:50",
        "active": 1,
        "distance": null
    }

As a bonus, if any could tell me why I can use the custom column in the orderBy, but not in the where on the model it would be appreciated.

1 Answers1

0

Nevermind, I figured it out. If you remove the appends variable and the accessor method, then it automatically appends it to the object. But I still can't figure out why I can't use distance in the where clause.

I actually found an answer to using the distance in the where clause as well here. Short answer, you have to do the computation again in the where clause as the custom column isn't know to the model.