簡體   English   中英

Laravel foreach數據庫問題

[英]Laravel foreach database issue

我正在建立一個論壇,當用戶單擊某個主題時,將顯示一個頁面,其中包含該主題的每個主題。 問題是,我該怎么做?

我為每個循環制作了一個循環,該循環顯示了數據庫中的所有主題,而不是屬於我單擊的主題的主題。 我怎樣才能做到這一點?

Web.php

Route::get('/', 'ThemesController@index')->name('home'); Route::get('/theme/{id}', 'ThemesController@show')->name('showtheme');

ThemesController.php

我只顯示show方法,因為我相信這是這里唯一需要的方法

public function show($id)
 { 
    $topics = Topic::all($);

    return view('themes/topics')->with('topics', $topics);
 }

topic.blade.php

@extends('layouts.default')

@section('content')
<div class="container main-content">
    <div class="row first-row">
        <div class="col s12">
            <div class="card">
                <div class="card-content clearfix"><a href="" class="btn blue-grey darken-4 right">Nieuw topic</a></div>
            </div>
        </div>
        <div class="col s12">
            <div class="card">
                <div class="card-content"><span class="card-title"> - Topics</span>
                    <div class="collection">
                        @foreach($topics as $topic)
                            <a href="" class="collection-item avatar collection-link"><img src="/uploads/avatars/{{ $topic->user->avatar }}" alt="" class="circle">
                                <div class="row">
                                    <div class="col s6">
                                        <div class="row last-row">
                                            <div class="col s12"><span class="title">Theme - {{ $topic->topic_title }}</span>
                                                <p>{!! str_limit($topic->topic_text, $limit = 100, $end = '...') !!}</p>
                                            </div>
                                        </div>
                                        <div class="row last-row">
                                            <div class="col s12 post-timestamp">Gepost door: {{ $topic->user->username }} op: {{  $topic->created_at }}</div>
                                        </div>
                                    </div>
                                    <div class="col s2">
                                        <h6 class="title center-align">Replies</h6>
                                        <p class="center replies">{{ $topic->replies->count() }}</p>
                                    </div>
                                    <div class="col s2">
                                        <h6 class="title center-align">Status</h6>
                                        <div class="status-wrapper center-align"><span class="status-badge status-open">open</span></div>
                                    </div>
                                    <div class="col s2">
                                        <h6 class="title center-align">Laatste reply</h6>
                                        <p class="center-align">Naam</p>
                                        <p class="center-align">Tijd</p>
                                    </div>
                                </div>
                            </a>
                        @endforeach
                    </div>
                </div>
            </div>
        </div>

        <div class="col s12">
            <div class="card">
                <div class="card-content clearfix"><a href="" class="btn blue-grey darken-4 right">Nieuw topic</a></div>
            </div>
        </div>
    </div>
</div>
@endsection

我相信我做的事情顯然是錯誤的,但我看不到。 幫助將不勝感激。 預先感謝如果我錯過了一些代碼,請通知我。

您如何定義主題與主題之間的關系? 您必須在數據庫中建立一對多或多對多的內容,才能正確查詢。 你有那樣的東西嗎?

****編輯****

public function show($id)
{ 
    $topics = Theme::find($id)->topics;
    return view('themes/topics')->with('topics', $topics);
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM