(メモ) Laravel5入門2 書き込み

## 参考

* [Laravel で掲示板作りたくなった - Qiita](http://qiita.com/localdisk/items/915ab90606fcdd469458)
* [Laravel で掲示板作りたくなった(2) - Qiita](http://qiita.com/localdisk/items/10f209252114b1a8b86d)
* [Laravel で掲示板作りたくなった(3) - Qiita](http://qiita.com/localdisk/items/d62327d064970635fcdd#_reference-b63855e1bafbcd79ebf2)

## 手順

* ubuntu18.04にて確認

```bash:
apt-get install -y composer
```

* `~/.config/composer/vendor/bin/` をPATHに追加しておく

```bash:
composer create-project laravel/laravel inspire-bbs-laravel
cd inspire-bbs-laravel
git init && git add -A && git commit -m init
```

```bash:
rm database/migrations/*.php
php artisan make:migration create_boards_table --create=boards
# InspireBBS と指定してなければ、Appとなっています。
php artisan app:name InspireBBS
git add -A && git commit -m "change namespaces"
```

```ruby:.env
DB_CONNECTION=sqlite
#DB_HOST=127.0.0.1
#DB_PORT=3306
#DB_DATABASE=homestead
#DB_USERNAME=homestead
#DB_PASSWORD=secret
```

```php:database/migrations/2016_*_create_boards_table.php
    public function up()
    {
        Schema::create('boards', function (Blueprint $table) {
            $table->increments('id');
            $table->string('name');
            $table->string('txt');
            $table->timestamps();
        });
    }
```

```php:database/seeds/DatabaseSeeder.php
    public function run()
    {
        DB::table('boards')->insert([
            'name' => '板の名前',
            'txt'  => '板の説明',
        ]);
    }
```

```bash:
touch database/database.sqlite
php artisan migrate
php artisan db:seed
echo ".tables" | sqlite3 database/database.sqlite
```

```bash:
php artisan make:model Models/Board
```

```php:routes/web.php
// Route::get('lists', function(\App\Models\Board $board) {
Route::get('lists', function(\InspireBBS\Models\Board $board) {
    return view('list', ['boards' => $board->all()]);
});
```

```html:resources/views/list.blade.php
    <table>
        @foreach($boards as $board)
        <tr>
            <td>{{ $board->name }}</td>
            <td>{{ $board->txt }}</td>
        </tr>
        @endforeach
    </table>

<form action="insert" method="POST">
    name: <input type="text" name="name">
    txt: <input type="text" name="txt">
    {{ csrf_field() }}
    <input type="submit">
</form>
```

```bash:webサーバ起動
php artisan serve --port 8888 --host 0.0.0.0
```

* http://localhost:8888/lists にアクセスし `boards` テーブルの中身が表示されることを確認。

----

## データの追加

```php:routes/web.php
Route::get('lists', function(\InspireBBS\Models\Board $board) {
    return view('list', ['boards' => $board->all()]);
});

Route::post('insert', function(\InspireBBS\Models\Board $board) {
    $board->name = Request::input('name');
    $board->txt = Request::input('txt');
    if(!empty($board->name) || !empty($board->txt)) {
        $board->save();
    }
    return Redirect::to('lists');
});
```

![3.png](https://qiita-image-store.s3.amazonaws.com/0/25728/ceaec798-055b-e65e-fe2a-6cd8e2cd1909.png)


* alertはなかった。

### viewへの変数の渡し方

* [Eloquent：リレーション 5.6 Laravel](https://readouble.com/laravel/5.6/ja/eloquent-relationships.html)

```php:routes/web.php(書き方1)
Route::get('lists', function() {
    $book = \App\Models\Book::find(1);
    return view('list', ['books' => $book->all()]);
});
```

```php:routes/web.php(書き方2)
Route::get('lists', function() {
    $book = \App\Models\Book::all();
    return view('list', ['books' => $book]);
});
```

```php:routes/web.php(書き方3)
Route::get('lists', function() {
    return view('list', ['books' => \App\Models\Book::all()]);
});
```

```php:routes/web.php(書き方4)
Route::get('lists', function() {
    $book = new \App\Models\Book;
    return view('list', ['books' => $book->all()]);
});
```

複数のtableをviewで使う場合

```php:routes/web.php
Route::get('lists', function() {
    return view('list'
        , ['books' => \App\Models\Book::all()]
        , ['authors' => \App\Models\Author::all()] );
});
```

```php:resources/views/list.blade.php
<table>
    @foreach($books as $book)
    <tr>
        <td>{{ $book->title }}</td>
    </tr>
    @endforeach
</table>

<table>
    @foreach($authors as $author)
    <tr>
        <td>{{ $author->name }}</td>
    </tr>
    @endforeach
</table>
```

保存

```php:routes/web.php
// 保存
Route::post('insert', function() {
    $book = new \App\Models\Book;
    $book->title = Request::input('title');
    if(!empty($book->title)) {
        $book->save();
    }
    return Redirect::to('lists');
});
```

```php:resources/views/list.blade.php
<h3>books</h3>
<table>
    @foreach($books as $book)
    <tr>
        <td>{{ $book->title }}</td>
    </tr>
    @endforeach
</table>

<h3>authors</h3>
<table>
    @foreach($authors as $author)
    <tr>
        <td>{{ $author->name }}</td>
    </tr>
    @endforeach
</table>


<h3>書き込みテスト</h3>
<form action="insert" method="POST">
    title: <input type="text" name="title">
    {{ csrf_field() }}
    <input type="submit">
</form>
```