yheihei / laravel_begginer

0 stars 0 forks source link

記事投稿モデルを作る #2

Open yheihei opened 4 years ago

yheihei commented 4 years ago

モデル作成コマンド

vagrant@tasaka:/var/www/html/laravel_lessons$ cd myblog/
vagrant@tasaka:/var/www/html/laravel_lessons/myblog$ pwd
/var/www/html/laravel_lessons/myblog
vagrant@tasaka:/var/www/html/laravel_lessons/myblog$ php artisan make:model Post --migration
Model created successfully.
Created Migration: 2020_04_12_055124_create_posts_table
yheihei commented 4 years ago

sqlite3の有効化

vagrant@tasaka:~$ sudo vim /etc/php/7.0/cli/php.ini 
extension=php_pdo.dll
extension=php_pdo_sqlite.dll
extension=php_sqlite3.dll
vagrant@tasaka:/var/www/html/laravel_lessons/myblog$ sudo apt-get update
vagrant@tasaka:/var/www/html/laravel_lessons/myblog$ sudo apt-get install php7.0-sqlite3
vagrant@tasaka:/var/www/html/laravel_lessons/myblog$ sudo service apache2 restart
yheihei commented 4 years ago

DBマイグレート

vagrant@tasaka:/var/www/html/laravel_lessons/myblog$ php artisan migrate
Migration table created successfully.
Migrating: 2020_04_12_055124_create_posts_table
Migrated:  2020_04_12_055124_create_posts_table
yheihei commented 4 years ago

テーブルが作られたか確認

vagrant@tasaka:/var/www/html/laravel_lessons/myblog$ sqlite3 database/database.sqlite 
SQLite version 3.11.0 2016-02-15 17:29:24
Enter ".help" for usage hints.
sqlite> .schema posts
CREATE TABLE "posts" ("id" integer not null primary key autoincrement, "title" varchar not null, "body" text not null, "created_at" datetime null, "updated_at" datetime null);
yheihei commented 4 years ago

記事が保存できるか確認

vagrant@tasaka:/var/www/html/laravel_lessons/myblog$ php artisan tinker
Psy Shell v0.9.12 (PHP 7.0.33-26+ubuntu16.04.1+deb.sury.org+1 — cli) by Justin Hileman
>>> $post = new App\Post();
=> App\Post {#2893}
>>> $post->title = 'title 1';
=> "title 1"
>>> $post->body = 'body 1';
=> "body 1"
>>> $post->save();
=> true

>>> App\Post::all();
=> Illuminate\Database\Eloquent\Collection {#2901
     all: [
       App\Post {#2902
         id: "1",
         title: "title 1",
         body: "body 1",
         created_at: "2020-04-12 07:51:47",
         updated_at: "2020-04-12 07:51:47",
       },
     ],
   }
>>> App\Post::all()->toArray();
=> [
     [
       "id" => 1,
       "title" => "title 1",
       "body" => "body 1",
       "created_at" => "2020-04-12 07:51:47",
       "updated_at" => "2020-04-12 07:51:47",
     ],
   ]
>>> exit
Exit:  Goodbye