yhbyun / nova-kakao-address
A Laravel Nova Kakao Address field.
Installs: 805
Dependents: 0
Suggesters: 0
Security: 0
Stars: 6
Watchers: 2
Forks: 1
Open Issues: 18
Language:Vue
Requires
- php: >=7.1.0
- dev-master
- v0.1.6
- v0.1.5
- v0.1.4
- v0.1.3
- v0.1.2
- v0.1.1
- v0.1.0
- dev-dependabot/npm_and_yarn/express-4.18.2
- dev-dependabot/npm_and_yarn/qs-6.5.3
- dev-dependabot/npm_and_yarn/decode-uri-component-0.2.2
- dev-dependabot/npm_and_yarn/loader-utils-1.4.2
- dev-dependabot/npm_and_yarn/async-2.6.4
- dev-dependabot/npm_and_yarn/url-parse-1.5.10
- dev-dependabot/npm_and_yarn/follow-redirects-1.14.8
- dev-dependabot/npm_and_yarn/path-parse-1.0.7
- dev-dependabot/npm_and_yarn/dns-packet-1.3.4
- dev-dependabot/npm_and_yarn/hosted-git-info-2.8.9
- dev-dependabot/npm_and_yarn/lodash-4.17.21
- dev-dependabot/npm_and_yarn/y18n-3.2.2
- dev-dependabot/npm_and_yarn/elliptic-6.5.4
- dev-dependabot/npm_and_yarn/ini-1.3.7
- dev-dependabot/npm_and_yarn/http-proxy-1.18.1
- dev-dependabot/npm_and_yarn/node-sass-4.14.1
- dev-dependabot/npm_and_yarn/websocket-extensions-0.1.4
- dev-dependabot/npm_and_yarn/mixin-deep-1.3.2
This package is auto-updated.
Last update: 2024-11-10 21:21:58 UTC
README
주소 입력시 주소에 해당하는 위도, 경도를 카카오 지도을 이용하여 입력할 수 있게 해주는 Laravel Nova Field
Installation
$ composer require yhbyun/nova-kakao-address
# config 파일을 수정하려면
$ php artisan vendor:publish --tag=nova-kakao-address
.env
파일에 카카오 API 키 추가
KAKAO_API_KEY=############################
사용방법
데이타베이스 post
테이블에 address
, latitide
, longitude
, address_1level
, address_2level
, address_3level
, address_others
컬럼이 있다고 가정하자.
app/post.php
<?php namespace App; use Illuminate\Database\Eloquent\Model; class Post extends Model { public static function boot() { parent::boot(); static::creating(function ($model) { if ($model->address) { $data = json_decode($model->address); $model->address = $data->address; $model->address_1level = $data->address_1level; $model->address_2level = $data->address_2level; $model->address_3level = $data->address_3level; $model->address_others = $data->address_others; $model->latitude = $data->latitude; $model->longitude = $data->longitude; } return true; }); static::updating(function ($model) { if ($model->address) { $data = json_decode($model->address); $model->address = $data->address; $model->address_1level = $data->address_1level; $model->address_2level = $data->address_2level; $model->address_3level = $data->address_3level; $model->address_others = $data->address_others; $model->latitude = $data->latitude; $model->longitude = $data->longitude; } else { $model->address = null; $model->address_1level = null; $model->address_2level = null; $model->address_3level = null; $model->address_others = null $model->latitude = null; $model->longitude = null; } return true; }); } ...
app\Nova\Post.php
<?php namespace App\Nova; use Laravel\Nova\Fields\ID; use Yhbyun\NovaKakaoAddress\KakaoAddress; class Post extends Resource { public static $model = 'App\Post'; public function fields(Request $request) { return [ ID::make()->sortable(), KakaoAddress::make('Address') ->rules('required') ->initLocation($this->latitude, $this->longitude), ]; } ...
동작원리
Laravel Nova 필드는 한개의 폼 값 만을 반환하기 때문에 다음과 같은 주소 정보를 갖는 json 객체를 반환하도록 구현했다.
{ "address": "전체주소", "address_1level": "시도", "address_2level": "구", "address_3level": "동", "address_others": "나머지 주소", "latitude": "위도", "longitude": "경도" }
해당 모델의 creating
, 'updating` 이벤트 시 address 속성에 이 json 데이터가 들어 있게 되고,
이 데이터로부터 전체주소, 시도, 구, 동, 나머지 주소, 위도, 경도 값을 구해서 해당 속성을 갱신한다.