Laravel foreach数据库问题

时间:2017-06-15 20:02:08

标签: php laravel laravel-5

我正在建立一个论坛,当用户点击主题时,页面会显示属于该主题的每个主题。问题是,我该怎么做?

我为每个循环创建了一个循环,它显示了数据库中的所有主题,而不是属于我单击的主题的主题。我怎么能这样做?

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);
 }

topics.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

我相信我做的事情显然是错的,但我看不出是什么。 帮助将不胜感激。提前致谢 如果我错过了一些代码,请通知我。

1 个答案:

答案 0 :(得分:1)

您如何定义主题与其主题之间的关系?您必须在数据库中建立一对多或多对多的东西才能正确查询。你还有类似的东西吗?

**** ****编辑

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