MongoDB์ ๋ฐ์ดํฐ ์ฐ๊ธฐ
์ปจํธ๋กค๋ฌ ํจ์๋ฅผ ํธ์ถํ๋ API ๊ฒฝ๋ก ์ถ๊ฐ
๋ค์ ๋ช ๋ น์ ์คํ ํ์ฌ API ๊ฒฝ๋ก ํ์ผ ์ ์์ฑํฉ๋๋ค.
php artisan install:api
ํ
Laravel 10.x๋ฅผ ์ฌ์ฉํ๋ ๊ฒฝ์ฐ ๋ช ๋ น์ด ์์ฑํ๋ ํ์ผ์ด ์ด๋ฏธ ์กด์ฌํ๋ฏ๋ก ์ด ๋จ๊ณ๋ฅผ ๊ฑด๋๋ฐ์ธ์.
์ปจํธ๋กค๋ฌ๋ฅผ ๊ฐ์ ธ์ค๊ณ routes/api.php
ํ์ผ์์ store()
๋ฉ์๋๋ฅผ ํธ์ถํ๋ API ๊ฒฝ๋ก๋ฅผ ์ถ๊ฐํฉ๋๋ค.
use App\Http\Controllers\MovieController; // ... Route::resource('movies', MovieController::class)->only([ 'store' ]);
๋ชจ๋ธ ํ๋ ์ ๋ฐ์ดํธ
๋ค์ ์ฝ๋์ ํ์๋ ๋๋ก app/Models
๋๋ ํ ๋ฆฌ์์ Movie
๋ชจ๋ธ์ ์
๋ฐ์ดํธํ์ฌ fill()
๋ฉ์๋๊ฐ ์ฑ์ฐ๋ ํ๋๋ฅผ ์ง์ ํฉ๋๋ค.
class Movie extends Model { protected $connection = 'mongodb'; protected $fillable = ['title', 'year', 'runtime', 'imdb', 'plot']; }
API์ ์์ฒญ ๊ฒ์
movie.json
ํ์ผ์ ๋ง๋ค๊ณ ๋ค์ ๋ฐ์ดํฐ๋ฅผ ์ฝ์
ํฉ๋๋ค.
{ "title": "The Laravel MongoDB Quick Start", "year": 2024, "runtime": 15, "imdb": { "rating": 9.5, "votes": 1 }, "plot": "This movie entry was created by running through the Laravel MongoDB Quick Start tutorial." }
shell์์ ๋ค์ ๋ช
๋ น์ ์คํํ์ฌ JSON ํ์ด๋ก๋๋ฅผ ์๋ํฌ์ธํธ์ POST
์์ฒญ์ผ๋ก ๋ณด๋
๋๋ค.
curl -H "Content-Type: application/json" --data @movie.json http://localhost:8000/api/movies
๋ฐ์ดํฐ ๋ณด๊ธฐ
์น ๋ธ๋ผ์ฐ์ ์์ http://127.0.0.1:8000/browse_movies ๋ฅผ ์ด์ด ์ ์ถํ ์ํ ์ ๋ณด๋ฅผ ํ์ธํฉ๋๋ค. ์ฝ์ ๋ ๋์์์ด ๊ฒฐ๊ณผ ์๋จ์ ๋ํ๋ฉ๋๋ค.
์ฐธ๊ณ
๋ฌธ์ ๊ฐ ๋ฐ์ํ๋ฉด MongoDB Community ํฌ๋ผ ์์ ๋์์ ์์ฒญํ๊ฑฐ๋ ํ์ด์ง ์ค๋ฅธ์ชฝ ๋๋ ์ค๋ฅธ์ชฝ ํ๋จ์ ์๋ Rate this page ํญ์ ์ฌ์ฉํ์ฌ ํผ๋๋ฐฑ์ ์ ์ถํ์ธ์.