■P037

────────────────
sail artisan make:controller Sample/IndexController
────────────────

●app/Http/Controllers/Sample/IndexController.php
────────────────
<?php

namespace App\Http\Controllers\Sample;

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

class IndexController extends Controller
{
    //
}
────────────────


■P038

●app/Http/Controllers/Sample/IndexController.php
────────────────
<?php

namespace App\Http\Controllers\Sample;

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

class IndexController extends Controller
{
    public function show()
    {
        return 'Hello';
    }
}
────────────────


●routes/web.php
────────────────
Route::get('/sample', [\App\Http\Controllers\Sample\IndexController::class, 'show']);
────────────────


■P039

●app/Http/Controllers/Sample/IndexController.php
────────────────
class IndexController extends Controller
{
    public function show()
    {
        return 'Hello';
    }
    public function showId($id)
    {
        return "Hello {$id}";
    }
}
────────────────

●routes/web.php
────────────────
Route::get('/sample/{id}', [\App\Http\Controllers\Sample\IndexController::class, 'showId']);
────────────────


■P040

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


■P041

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

namespace App\Http\Controllers\Tweet;

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

class IndexController extends Controller
{
    /**
     * Handle the incoming request.
     */
    public function __invoke(Request $request)
    {
        //
    }
}
────────────────


●app/Http/Controllers/Tweet/IndexController.php
────────────────
    public function __invoke(Request $request)
    {
        return 'Single Action!';
    }
────────────────


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


■P042

────────────────
<?php
$javaScriptCode = "<script>alert('xss');</script>";
?>
{{ $javaScriptCode }}
────────────────


■P043

────────────────
<?php
$javaScriptCode = "<script>alert('xss');</script>";
?>
{!! $javaScriptCode !!}
────────────────


■P044

●resources/views/tweet/index.blade.php
────────────────
<!doctype html>
<html lang="ja">
<head>
    <meta charset="UTF-8">
    <meta name="viewport"
          content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
    <title>つぶやきアプリ</title>
</head>
<body>
    <h1>つぶやきアプリ</h1>
		<p>{{ $name }}</p>
</body>
</html>
────────────────

●app/Http/Controllers/Tweet/IndexController.php
────────────────
    public function __invoke(Request $request)
    {
        return view('tweet.index', ['name' => 'laravel']);
    }
────────────────


■P045

────────────────
use Illuminate\Support\Facades\View;

class IndexController extends Controller
{
    public function __invoke(Request $request)
    {
        return View::make('tweet.index', ['name' => 'laravel']);
    }
}
────────────────


■P046

────────────────
use Illuminate\View\Factory;

class IndexController extends Controller
{
    public function __invoke(Request $request, Factory $factory)
    {
        return $factory->make('tweet.index', ['name' => 'laravel']);
    }
}
────────────────

────────────────
return view('tweet.index')->with('name', 'laravel');
────────────────

────────────────
return view('tweet.index')
            ->with('name', 'laravel')
            ->with('version', '12');
────────────────


■P047

────────────────
sail mysql
────────────────


────────────────
select version();
────────────────


■P048

────────────────
show databases;
────────────────

────────────────
show tables from laravel;
────────────────


■P049

────────────────
sail artisan make:migration create_tweets_table
────────────────

●database/migrations/yyyy_mm_dd_xxxxxx_create_tweets_table.php
────────────────
<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

return new class extends Migration
{
    /**
     * Run the migrations.
     */
    public function up(): void
    {
        Schema::create('tweets', function (Blueprint $table) {
            $table->id();
            $table->timestamps();
        });
    }

    /**
     * Reverse the migrations.
     */
    public function down(): void
    {
        Schema::dropIfExists('tweets');
    }
};
────────────────


■P050

●database/migrations/yyyy_mm_dd_◯◯◯◯_create_tweets_table.php
────────────────
    public function up(): void
    {
        Schema::create('tweets', function (Blueprint $table) {
            $table->id();
            $table->string('content');
            $table->timestamps();
        });
    }
────────────────

────────────────
https://laravel.com/docs/13.x/migrations#available-column-types
────────────────

────────────────
sail artisan migrate
────────────────


■P051

────────────────
sail mysql
────────────────

────────────────
show tables from laravel;
────────────────

────────────────
use laravel;
────────────────

────────────────
show columns from tweets;
────────────────


■P052

────────────────
sail artisan make:seeder TweetsSeeder
────────────────

●database/seeders/TweetsSeeder.php
────────────────
<?php

namespace Database\Seeders;

use Illuminate\Database\Console\Seeds\WithoutModelEvents;
use Illuminate\Database\Seeder;

class TweetsSeeder extends Seeder
{
    /**
     * Run the database seeds.
     */
    public function run(): void
    {
        //
    }
}
────────────────


■P053

●database/seeders/TweetsSeeder.php
────────────────
<?php

namespace Database\Seeders;

use Illuminate\Database\Console\Seeds\WithoutModelEvents;
use Illuminate\Database\Seeder;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Str;

class TweetsSeeder extends Seeder
{
    public function run(): void
    {
        DB::table('tweets')->insert([
            'content' => Str::random(100),
            'created_at' => now(),
            'updated_at' => now(),
        ]);
    }
}
────────────────


■P054

●database/seeders/DatabaseSeeder.php
────────────────
    public function run(): void
    {
        // 初期値のコードは使用しないので削除して、下記の1行にする
        $this->call([TweetsSeeder::class]);
    }
────────────────

────────────────
sail artisan db:seed
────────────────

────────────────
sail artisan db:seed --class=TweetsSeeder
────────────────


■P055

────────────────
sail mysql
────────────────

────────────────
use laravel;
────────────────

────────────────
select * from tweets;
────────────────

────────────────
truncate table tweets;
────────────────

────────────────
sail artisan make:model Tweet
────────────────


■P056

────────────────
sail artisan make:model Tweet --migration
────────────────
sail artisan make:model Tweet -m
────────────────

────────────────
sail artisan make:model Tweet --factory
────────────────
sail artisan make:model Tweet -f
────────────────

────────────────
sail artisan make:model Tweet --seed
────────────────
sail artisan make:model Tweet -s
────────────────

────────────────
sail artisan make:model Tweet -mfsc
────────────────

────────────────
sail artisan make:model Tweet --pivot
────────────────
sail artisan make:model Tweet -p
────────────────


■P057

────────────────
https://laravel.com/docs/13.x/eloquent#generating-model-classes
────────────────


●app/Models/Tweet.php
────────────────
<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;

class Tweet extends Model
{
    //
}
────────────────

────────────────
use Illuminate\Database\Eloquent\Attributes\Table;
use Illuminate\Database\Eloquent\Model;
 
#[Table('tweets')]
class Tweet extends Model
{
}
────────────────


■P058

────────────────
#[Table(key: 'tweet_id')]
class Tweet extends Model
────────────────

────────────────
#[Table(key: 'uuid', keyType: 'string', incrementing: false)]
class Tweet extends Model
────────────────

────────────────
#[Table(timestamps: false)]
public $timestamps = false;
────────────────

────────────────
const CREATED_AT = 'creation_date';
const UPDATED_AT = 'updated_date';
────────────────


■P059

────────────────
sail artisan make:factory TweetFactory --model=Tweet
────────────────

●database/factories/TweetFactory.php
────────────────
<?php

namespace Database\Factories;

use App\Models\Tweet;
use Illuminate\Database\Eloquent\Factories\Factory;

/**
 * @extends Factory<Tweet>
 */
class TweetFactory extends Factory
{
    /**
     * Define the model's default state.
     *
     * @return array<string, mixed>
     */
    public function definition(): array
    {
        return [
            //
        ];
    }
}
────────────────


■P060

●database/factories/TweetFactory.php
────────────────
    public function definition(): array
    {
        return [
            'content' => $this->faker->realText(100),
        ];
    }
────────────────

────────────────
https://github.com/FakerPHP/Faker
────────────────

●config/app.php
────────────────
'faker_locale' => env('APP_FAKER_LOCALE', 'en_US'),
────────────────


●.env
────────────────
APP_FAKER_LOCALE=ja_JP
────────────────


■P061

●database/seeders/TweetsSeeder.php
────────────────
<?php

namespace Database\Seeders;

use App\Models\Tweet;
use Illuminate\Database\Seeder;

class TweetsSeeder extends Seeder
{
    /**
     * Run the database seeds.
     */
    public function run(): void
    {
        Tweet::factory()->count(10)->create();
    }
}
────────────────

●app/Models/Tweet.php
────────────────
<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

class Tweet extends Model
{
    use HasFactory;
}
────────────────


■P062

────────────────
use Illuminate\Database\Eloquent\Attributes\Table;
use Illuminate\Database\Eloquent\Attributes\UseFactory;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

#[Table('tweets')]
#[UseFactory(OtherFactory::class)]
class Tweet extends Model
{
    use HasFactory;
}
────────────────

────────────────
sail artisan db:seed
────────────────

────────────────
sail mysql
────────────────

────────────────
use laravel;
────────────────

────────────────
select * from tweets;
────────────────


■P063

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

namespace App\Http\Controllers\Tweet;

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

class IndexController extends Controller
{
    public function __invoke(Request $request)
    {
        $tweets = Tweet::all();
        dd($tweets);
        return view('tweet.index', ['name' => 'laravel']);
    }
}
────────────────


■P064

●app/Http/Controllers/Tweet/IndexController.php
────────────────
        $tweets = Tweet::all();

        return view('tweet.index')
            ->with('tweets', $tweets);
────────────────


■P065

●resources/views/tweet/index.blade.php
────────────────
<body>
    <h1>つぶやきアプリ</h1>
    <div>
    @foreach($tweets as $tweet)
        <p>{{ $tweet->content }}</p>
    @endforeach
    </div>
</body>
────────────────


■P066
────────────────
https://laravel.com/docs/13.x/blade
────────────────
