■P067

────────────────
sail artisan make:controller Tweet/CreateController --invokable
────────────────

────────────────
sail artisan make:request Tweet/CreateRequest
────────────────


■P068

●app/Http/Requests/Tweet/CreateRequest.php
────────────────
<?php

namespace App\Http\Requests\Tweet;

use Illuminate\Contracts\Validation\ValidationRule;
use Illuminate\Foundation\Http\FormRequest;

class CreateRequest extends FormRequest
{
    /**
     * Determine if the user is authorized to make this request.
     */
    public function authorize(): bool
    {
        return false;
    }

    /**
     * Get the validation rules that apply to the request.
     *
     * @return array<string, ValidationRule|array<mixed>|string>
     */
    public function rules(): array
    {
        return [
            //
        ];
    }
}
────────────────


■P069

●app/Http/Requests/Tweet/CreateRequest.php
────────────────
    public function authorize(): bool
    {
        return true;
    }

    public function rules(): array
    {
        return [
            'tweet' => 'required|max:140',
        ];
    }
────────────────

────────────────
https://laravel.com/docs/13.x/validation#available-validation-rules
────────────────


■P070

●app/Http/Controllers/Tweet/CreateController.php
────────────────
<?php

namespace App\Http\Controllers\Tweet;

use App\Http\Controllers\Controller;
use App\Http\Requests\Tweet\CreateRequest;

class CreateController extends Controller
{
    public function __invoke(CreateRequest $request)
    {
        //
    }
}
────────────────


●routes/web.php
────────────────
Route::post('/tweet', \App\Http\Controllers\Tweet\CreateController::class);
────────────────

■P071

●routes/web.php
────────────────
Route::get('/tweet', \App\Http\Controllers\Tweet\IndexController::class)->name('tweet.index');
Route::post('/tweet', \App\Http\Controllers\Tweet\CreateController::class)->name('tweet.create');
────────────────

●resources/views/tweet/index.blade.php
────────────────
<body>
    <h1>つぶやきアプリ</h1>
    <div>
        <p>投稿フォーム</p>
        <form action="{{ route('tweet.create') }}" method="post">
            @csrf
            <label for="tweet-content">つぶやき</label>
            <span>140文字まで</span>
            <textarea id="tweet-content" type="text" name="tweet" placeholder="つぶやきを入力"></textarea>
            <button type="submit">投稿</button>
        </form>
    </div>
    <div>
        @foreach($tweets as $tweet)
            <p>{{ $tweet->content }}</p>
        @endforeach
    </div>
</body>
────────────────


■P072

────────────────
<form action="{{ route('tweet.create') }}" method="post">
────────────────

────────────────
<textarea id="tweet-content" type="text" name="tweet" placeholder="つぶやきを入力"></textarea>
────────────────

────────────────
<input type="hidden" name="_token" value="xZPyILblC4GXVFqP3h6ZXQpoQtts8mzsJsAm4YBi">
────────────────


■P073

●resources/views/tweet/index.blade.php
────────────────
            <textarea id="tweet-content" type="text" name="tweet" placeholder="つぶやきを入力"></textarea>
            @error('tweet')
            <p style="color: red;">{{ $message }}</p>
            @enderror
────────────────

────────────────
@error('email', 'login')
────────────────


■P074

●config/app.php
────────────────
'locale' => env('APP_LOCALE', 'en'),
'fallback_locale' => env('APP_FALLBACK_LOCALE', 'en'),
────────────────

●.env
────────────────
APP_LOCALE=ja
APP_FALLBACK_LOCALE=ja
────────────────

────────────────
sail composer require laravel-lang/lang:~15.29.2
────────────────


■P075

────────────────
sail artisan lang:add ja
────────────────

●lang/ja/validation.php
────────────────
    'attributes' => [
        'tweet' => 'つぶやき',
    ],
];
────────────────


■P076

●app/Http/Requests/Tweet/CreateRequest.php
────────────────
    public function tweet(): string
    {
    return $this->input('tweet');
    }
────────────────


●app/Http/Controllers/Tweet/CreateController.php（全文）
────────────────
<?php

namespace App\Http\Controllers\Tweet;

use App\Http\Controllers\Controller;
use App\Http\Requests\Tweet\CreateRequest;
use App\Models\Tweet;

class CreateController extends Controller
{
    /**
     * Handle the incoming request.
     */
    public function __invoke(CreateRequest $request)
    {
        $tweet = new Tweet;
        $tweet->content = $request->tweet();
        $tweet->save();

        return redirect()->route('tweet.index');
    }
}
────────────────


■P078

●app/Http/Controllers/Tweet/IndexController.php
────────────────
$tweets = Tweet::query()->orderBy('created_at', 'DESC')->get();
────────────────

────────────────
$tweets = Tweet::all()->sortByDesc('created_at');
────────────────

