次のページにほぼ従っています。
Laravel5.6入門 基本CRUD操作を体で覚える
相違点は、データベースを、MariaDB にしたことです。
Laravel Framework 10.37.3 を使いました。
Ubuntu で必要なソフトのインストール
sudo apt install php-mysql
- MariaDB の用意
- プロジェクトの作成
- .env の設定
- モデルとマイグレーションを作成
- マイグレーションファイルを編集 2行追加
- app/Providers/AppServiceProvider.php の修正 2行追加 >この修正は、MariaDB との接続に必要です。
- マイグレーションファイルの内容をデータベースに反映
- コントローラーの作成
- ルーティングを定義 >最後に一行を追加
- MariaDB のテーブル articles にデータを入れる。 次のような SQL で入れます。
- 一覧を表示するように、コントローラーを作成
- サーバーを起動
- curl でアクセスして JSON でデータの一覧が返ってくることを確認
MariaDB に次のデータを用意します。
User: scott
Password: tiger123
データベース: article
接続の確認
$ mysql -uscott -ptiger123 article
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MariaDB connection id is 36
Server version: 10.5.15-MariaDB-0ubuntu0.21.10.1 Ubuntu 21.10
Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
MariaDB [article]>
laravel new sample
もしも .env がない時は、
cp .env.example .env
.env のデータベースの部分を修正
.env
(省略)
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=article
DB_USERNAME=scott
DB_PASSWORD=tiger123
(省略)
もしも、
APP_KEY=
と空白の時は、
php artisan key:generate
次のように .env の APP_KEY が設定される
APP_KEY=base64:xjG4svW8IkhyMg0NzCP+C4kVZGvs/KUO2XarVzJJdUg=
php artisan make:model Article -m
-m をつけることで、
database/migrations/2021_05_15_002753_create_articles_table.php が作成されます。
database/migrations/2021_05_15_002753_create_articles_table.php
// 略
public function up()
{
Schema::create('articles', function (Blueprint $table) {
$table->id();
$table->string('title'); // 追加
$table->text('body'); // 追加
$table->timestamps();
});
// 略
app/Providers/AppServiceProvider.php
<?php
namespace App\Providers;
use Illuminate\Support\ServiceProvider;
use Illuminate\Support\Facades\Schema; // 追加
class AppServiceProvider extends ServiceProvider
{
/**
* Bootstrap any application services.
*
* @return void
*/
public function boot()
{
Schema::defaultStringLength(191); // 追加
}
// 略
php artisan migrate
MariaDB は次のようになります。
MariaDB [article]> show tables;
+-------------------+
| Tables_in_article |
+-------------------+
| articles |
| migrations |
| password_resets |
| users |
+-------------------+
4 rows in set (0.03 sec)
MariaDB [article]>
php artisan make:controller ArticlesController -r
routes/web.php
// 略
Route::resource('articles', 'App\Http\Controllers\ArticlesController');
insert into articles set id=10, title="aaa", body="こんにちは";
insert into articles set id=11, title="bbb", body="おはよう";
insert into articles set id=12, title="ccc", body="今晩は";
MariaDB の状態
MariaDB [article]> select * from articles;
+----+------------+------------+-------+-----------------+
| id | created_at | updated_at | title | body |
+----+------------+------------+-------+-----------------+
| 10 | NULL | NULL | aaa | こんにちは |
| 11 | NULL | NULL | bbb | おはよう |
| 12 | NULL | NULL | ccc | 今晩は |
+----+------------+------------+-------+-----------------+
3 rows in set (0.000 sec)
MariaDB [article]>
app/Http/Controllers/ArticlesController.php
// 略
use App\Models\Article;
// 略
public function index()
{
$articles = Article::all();
return $articles;
}
// 略
php artisan serve
curl http://127.0.0.1:8000/articles | jq .
[
{
"id": 10,
"title": "aaa",
"body": "こんにちは",
"created_at": null,
"updated_at": null
},
{
"id": 11,
"title": "bbb",
"body": "おはよう",
"created_at": null,
"updated_at": null
},
{
"id": 12,
"title": "ccc",
"body": "今晩は",
"created_at": null,
"updated_at": null
}
]
Laravel のバージョンの確認
$ php artisan --version
Laravel Framework 10.37.3