0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

入社4日目とりあえず2ch掲示板に機能を実装してみる

Last updated at Posted at 2025-01-09

前回かなり時間をかけて記事の作成をしたが、知識習得のためのアウトプットが優先されるのでもっとかいつまんで行く。

テーブル作成

スレッド用マイグレーション
ファイル:create_threads_table.php

public function up()
{
    Schema::create('threads', function (Blueprint $table) {
        $table->id(); // スレッドID
        $table->string('title'); // スレッドタイトル
        $table->timestamps(); // 作成日時・更新日時
    });
}

これを入れ込んだ。

Schema::create

というメソッドでテーブル作成

('threads', function (Blueprint $table) {

(テーブル名,カラム作るぞ)
Blueprintクラスの設計図をつかって$tableを作ってくぞという認識。

こんな感じで投稿用のマイグレーションファイルにも追加していく。

public function up()
{
    Schema::create('posts', function (Blueprint $table) {
        $table->id(); // 投稿ID
        $table->foreignId('thread_id')->constrained()->onDelete('cascade'); // スレッドID
        $table->text('content'); // 投稿内容
        $table->timestamps(); // 作成日時・更新日時
    });
}

マイグレーションファイルができたらDBに反映させる。

php artisan migrate




mysql -u root
USE bulletin_board;
SHOW TABLES;

これで出力ができていれば完了

モデルとコントローラー作成

php artisan make:model Thread
php artisan make:model Post

こんなにもターミナルを使うと思ってなかった笑
もうVSコードとかGUIを駆使して全て賄えるものだと思っていた。。。。

Thread モデル

これを記述

namespace App\Models;

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

class Thread extends Model
{
    use HasFactory;

    protected $fillable = ['title'];

    public function posts()
    {
        return $this->hasMany(Post::class);
    }
}

モデルはデータベーステーブルと連携するアーキテクチャ。

namespace App\Models;

わたしはここにいるよ!という居場所の指定。
クラス名の重複を防ぐんだとか。

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

ここの階層の機能使うよー

class Thread extends Model

Thread クラスがさっきインポートしたModelの機能を継承

public function posts()
{
    return $this->hasMany(Post::class);
}

コントローラーからでもビューからでも呼び出しできるよ。
Postと一対多にするよ。

Postモデルも同じく記述していく。

コントローラーの作成

php artisan make:controller ThreadController --resource
php artisan make:controller PostController --resource

コントローラー作って
この中身も作成していく。

namespace App\Http\Controllers;

use App\Models\Thread;
use Illuminate\Http\Request;

class ThreadController extends Controller
{
    // スレッド一覧を表示する
    public function index()
    {
        $threads = Thread::all(); // データベースからすべてのスレッドを取得
        return view('threads.index', compact('threads')); // ビューに渡す
    }
}

use はこの機能をインポートするよ
そしてThreadControllerに継承すると言っている。

'threads.index'

resources/views/threads/index.blade.php 

を指定。

compact('threads')

連想配列としてthreadsを生成する

ルーティングの設定

routes/web.phpに

use App\Http\Controllers\ThreadController;

Route::get('/', [ThreadController::class, 'index']); // トップページにスレッド一覧を表示
Route::resource('threads', ThreadController::class); // RESTfulなスレッドのルートを生成

アプリケーションのトップページ(/)にアクセスがあったときに ThreadController の index メソッドを呼び出し。

Route::resourceを使うとCRUDの機能を一括で定義できるらしい。

ビューの作成

resources/views/threads/

index.blade.php
を作成。

<!DOCTYPE html>
<html lang="ja">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>スレッド一覧</title>
</head>
<body>
    <h1>スレッド一覧</h1>
    <ul>
        @foreach ($threads as $thread)
            <li>{{ $thread->title }}</li>
        @endforeach
    </ul>
</body>
</html>

これは簡単だった。
さっきあったコントローラーの値がここに飛んでくる仕様。

 @foreach ($threads as $thread)
            <li>{{ $thread->title }}</li>
        @endforeach

($threads as $thread)

$threadsに来たものを一つずつ$threadにいれるよ。

$thread->title

これを表示。
それをforeachで繰り返すよ。

ということ。

これでとりあえずデータベースに保存されているtitleを呼び出すことができた。

/にアクセスするとルーティングがコントローラーを呼ぶ。
コントローラーがモデルで設定したルールを元に処理を開始。
それをビューで表示する。

ここまでは理解できたぞ。
実は今はもうすこし先を作業している。
作業中は理解が追いつかないけど振り返ると理解できてくる。

進めながら復習兼ねて書いていく!

頑張れ私

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?