3
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Laravel で MongoDB の API を作成

Last updated at Posted at 2018-06-22

Laravel で MongoDB の collection を読む API を作成します。

MongoDB のデータは、次のものと同じです。
Laravel で MongoDB のデータをテーブル表示
次のライブラリーを使います。
jenssegers/laravel-mongodb

  1. プロジェクトの作成
laravel new api02
cd api02
composer require jenssegers/mongodb
  1. config/app.php の編集
'providers' => [
// 略
        Jenssegers\Mongodb\MongodbServiceProvider::class,
 ],

    'aliases' => [
// 略
         'Moloquent' => Jenssegers\Mongodb\Eloquent\Model::class,
 ],
  1. .env の編集
#DB_CONNECTION=mysql
#DB_HOST=127.0.0.1
#DB_PORT=3306
#DB_DATABASE=homestead
#DB_USERNAME=homestead
#DB_PASSWORD=secret
DB_CONNECTION=mongodb
DB_HOST=127.0.0.1
DB_PORT=27017
DB_DATABASE=city_db
  1. config/database.php の編集
'default' => env('DB_CONNECTION', 'mongodb'),
    // 略
    'connections' => [
        'mongodb' => [
            'driver' => 'mongodb',
            'host' => env('DB_HOST'),
            'port' => env('DB_PORT'),
            'database' => env('DB_DATABASE'),
        'username' => env('DB_USERNAME'),
        'password' => env('DB_PASSWORD'),
        ],
    ],
  1. キャッシュのクリア
php artisan cache:clear
  1. モデルの作成
mkdir app/Models
php artisan make:model Models/City
  1. モデルの編集
app/Models/City.php
<?php

namespace App\Models;


class City extends \Moloquent
{
protected $collection = 'saitama';
}
  1. routes/api.php の編集
routes/api.php
// 略
Route::group(['middleware' => ['api']], function(){
  Route::resource('city', 'Api\CityController');
  Route::get('all', 'Api\CityController@all');
  Route::get('find/{id}', 'Api\CityController@find');
  Route::get('where/{name}', 'Api\CityController@where');
  Route::get('between/{min}/{max}', 'Api\CityController@between');
});
  1. コントローラーの作成
php artisan make:controller Api/CityController
  1. コントローラーの編集
app/Http/Controllers/Api/CityController.php
<?php

namespace App\Http\Controllers\Api;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Models\City;

class CityController extends Controller
{
        public function index()
        {
        $cities = City::orderBy('_id', 'desc')->get();
	return $cities;
        }

        public function all()
        {
        $cities = City::all();
        return $cities;
        }

        public function find($id)
        {
        $cities = City::find($id);
        return $cities;
        }

        public function where($name)
        {
        $cities = City::where('name','=',$name)->get();
        return $cities;
        }

        public function between($min,$max)
        {
        $cities = City::whereBetween('population',[(int)$min,(int)$max])->get();
        return $cities;
        }
}
  1. サーバーの起動
php artisan serve --host 0.0.0.0
  1. クライアントでアクセス
curl http://localhost:8000/api/city
curl http://localhost:8000/api/all
curl http://localhost:8000/api/find/5b2c483e6fc1214d33e6151c
curl http://localhost:8000/api/where/久喜
curl http://localhost:8000/api/between/30000/50000
3
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?