0

I am making a forum and when the user clicks on a theme, a page shows up with every topic that belongs to that theme. The problem is, how do I do this?

I made a for each loop which shows ALL the topics from the database instead of the topics that belong to that theme I clicked on. How can I do this?

Web.php

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

ThemesController.php

I only show the show method because I believe that's the only one necessary here

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

I believe I'm doing something obviously wrong but I can't see what. Help will be appreciated. Thanks in advance If I miss some code, Please inform me.

1 Answer 1

0

How are you defining the relationship between a theme and it's topics? You'll have to have something like a one to many or many to many established in your database for you to be able to query correctly. Do you have anything like that yet?

****edit****

public function show($id)
{ 
    $topics = Theme::find($id)->topics;
    return view('themes/topics')->with('topics', $topics);
}
Sign up to request clarification or add additional context in comments.

8 Comments

I do. My relation between theme and topic is the following: 1. A theme belongs to a user and has many topics 2. A Topic belongs to a user and belongs to a theme but the topic also has many replies. Those are the relations i have.
In that case you should be able to get the topics you need by accessing the user's topics directly, such as Auth::user()->topics, and pass that into the view like ->with('topics', Auth::user()->topics);
That's is I believe not what I meant. If the user clicks on a theme. the next page needs to be a foreach loop which displays all the topics from that theme. I don't think the code that you provided is capable of doing that if I'm correct.
I didn't understand you correctly but now I do. In that case you should pass the selected theme id to something like ThemeController@show and have the theme query for it's related topics. That'll keep things separated and simple.
So if i understand you correctly i have to do something like this $topics = Topic::all($id); return view('themes/topics')->with('topics', $topics); in my show method
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.