tiger9810 / Laravel-Chirper

0 stars 0 forks source link

Databaseに保存したChirpsの表示 #9

Open tiger9810 opened 1 year ago

tiger9810 commented 1 year ago

チャープを取得する(参考:Databaseに保存したChirpsの表示)

すべてのユーザーからインデックス ページに Chirps を渡すようにクラスindexのメソッドを更新しましょう。 app/Http/Controllers/ChirpController.php(参考)(説明)

 public function index(): View
    {
        return view('chirps.index', [
            'chirps' => Chirp::with('user')->latest()->get(),
        ]);
    }

User情報とChirpsを接続する

UserがChirpsをどう持っているかの関係性はhas manyで追加したので、次はChirpsがUserをどう持っているかを定義します。これにより、ChirpモデルはUserモデルに所属することを表現します。 UserがChirpsを「has many(複数所有)」の関係性で持っている場合、次はChirpsがUserを「belongs to(所属する)」の関係性で持つように定義する必要がある、ということです。

つまり、Userモデルに「has many」の関係性を追加した後、Chirpモデルには「belongs to」の関係性を追加する必要があります。これにより、ChirpモデルはUserモデルに所属することを表現します。 app/Models/Chirp.php

use Illuminate\Database\Eloquent\Relations\BelongsTo;
class Chirp extends Model
{
 ...
    public function user(): BelongsTo
    {
        return $this->belongsTo(User::class);
    }
}

chirps.indexの変更

chirps.indexでChirpsを表示できるようにする。(参考)

tiger9810 commented 1 year ago

'chirps' => Chirp::with('user')->latest()->get()の部分では、'chirps'というキーにChirpモデルを含むデータを関連付けています。このデータは、Chirp::with('user')->latest()->get()のクエリによって取得されます。

以下に、それぞれの機能について説明します:

Chirp::with('user'): Chirpモデルに対してuserというリレーションシップを指定しています。この関連付けにより、Chirpモデルを取得する際に関連するUserモデルの情報も同時に取得できます。具体的には、Userモデルのデータを含んだChirpモデルのコレクションが返されます。

latest(): Chirpモデルのクエリに対してlatest()スコープを適用しています。latest()は、created_atカラムを基準に最新のレコードから順にデータを取得するためのメソッドです。このようにすることで、時系列順に並んだデータを取得できます。

get(): クエリを実行して、該当するデータを取得します。get()メソッドはクエリを実行し、結果としてChirpモデルのコレクションを返します。

したがって、'chirps' => Chirp::with('user')->latest()->get()は、'chirps'というキーにChirpモデルを含むデータを関連付けています。このデータは、関連するUserモデルの情報を含み、最新の順に並んでいます。このデータは後でビューに渡され、表示や処理に使用されることが期待されます。

tiger9810 commented 1 year ago

resources/views/chirps/index.blade.php

<x-app-layout>
    <div class="max-w-2xl mx-auto p-4 sm:p-6 lg:p-8">
        <form method="POST" action="{{ route('chirps.store') }}">
            @csrf
            <textarea
                name="message"
                placeholder="{{ __('What\'s on your mind?') }}"
                class="block w-full border-gray-300 focus:border-indigo-300 focus:ring focus:ring-indigo-200 focus:ring-opacity-50 rounded-md shadow-sm"
            >{{ old('message') }}</textarea>
            <x-input-error :messages="$errors->store->get('message')" class="mt-2" />
            <x-primary-button class="mt-4">{{ __('Chirp') }}</x-primary-button>
        </form>

        <div class="mt-6 bg-white shadow-sm rounded-lg divide-y">
            @foreach ($chirps as $chirp)
                <div class="p-6 flex space-x-2">
                    <svg xmlns="http://www.w3.org/2000/svg" class="h-6 w-6 text-gray-600 -scale-x-100" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2">
                        <path stroke-linecap="round" stroke-linejoin="round" d="M8 12h.01M12 12h.01M16 12h.01M21 12c0 4.418-4.03 8-9 8a9.863 9.863 0 01-4.255-.949L3 20l1.395-3.72C3.512 15.042 3 13.574 3 12c0-4.418 4.03-8 9-8s9 3.582 9 8z" />
                    </svg>
                    <div class="flex-1">
                        <div class="flex justify-between items-center">
                            <div>
                                <span class="text-gray-800">{{ $chirp->user->name }}</span>
                                <small class="ml-2 text-sm text-gray-600">{{ $chirp->created_at->format('j M Y, g:i a') }}</small>
                            </div>
                        </div>
                        <p class="mt-4 text-lg text-gray-900">{{ $chirp->message }}</p>
                    </div>
                </div>
            @endforeach
        </div>
    </div>
</x-app-layout>