Laravel links and web.php routes messed up

I am making a forum with themes and themes. If a user clicks on a topic, he / she gets all the topics of that topic. This is where we face the first problem. As theme.blade.php

I have a headline <span class="card-title">{{ $theme->theme_title }} - Topics</span>

. This heading should show the name of the topic the user has clicked on. But it does show (just a wild guess) some random topic title from a database that is not even related to that topic.

Now I have done an additional view for the user. If the user clicks on a topic from the selected topic. He / she should redirect to the topic he clicked on, but instead shows (again) some random topics from the database that are not related to the topic / topic at all. Instead of the topic the user clicked on. In this GIF http://imgur.com/a/vOQFT you can see the problem. If you look at the profile picture and username. Maybe the problem is in Web.php

or somewhere else, I don't know. Sorry for the long story, but I couldn't figure out how to put this better. I think I have switched some things in the code.

Here is every code file that this issue might appear in.

Web.php

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


Route::get('/theme/{theme_id}/topics/{topic_id}', 'TopicsController@show')->name('showtopic');



Route::group(['middleware' => 'App\Http\Middleware\AdminMiddleware'], function() {

//THEMES

Route::get('/theme/{theme_id}/edit', 'ThemesController@edit')->name('edittheme');
Route::patch('/theme/{theme_id}/edit', 'ThemesController@update')->name('updatetheme');

Route::get('/theme/create', 'ThemesController@create')->name('createtheme');
Route::post('/theme/create', 'ThemesController@save')->name('savetheme');

Route::delete('/theme/{theme_id}/delete', 'ThemesController@destroy')->name('deletetheme');

//TOPICS

Route::get('/theme/{theme_id}/topics/{topic_id}/edit', 'TopicsController@edit')->name('edittopic');
Route::patch('/theme/{theme_id}/topics/{topic_id}/edit', 'TopicsController@update')->name('updatetopic');

Route::get('/theme/{theme_id}/topics/create', 'TopicsController@create')->name('createtopic');
Route::post('/theme/{theme_id}/topics/create', 'TopicsController@save')->name('savetopic');

Route::delete('/theme/{theme_id}/topics/{topic_id}/delete', 'TopicsController@destroy')->name('deletetopic');



});

Route::get('user/profile', 'UserController@profile')->name('showprofile');
Route::post('user/profile', 'UserController@update_avatar');

      

Theme.blade.php (List of each theme in the theme)

<div class="col s12">
            <div class="card">
                <div class="card-content"><span class="card-title">{{ $theme->theme_title }} - Topics</span>
                    <div class="collection">
                        @foreach($topics as $topic)
                            <a href="{{ route('showtopic', ['theme_id' => $theme->id, 'topic_id' => $topic->id ]) }}" 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="card-title">{{ $topic->topic_title }}</span>
                                                <p>{!! str_limit($topic->topic_text, $limit = 125, $end = '...') !!}</p>
                                            </div>
                                        </div>
                                        <div class="row last-row">
                                            <div class="col s12 post-timestamp">Posted by: {{ $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">Last reply</h6>
                                        <p class="center-align"></p>
                                        <p class="center-align">Tijd</p>
                                    </div>
                                </div>
                            </a>
                        @endforeach
                    </div>
                </div>
            </div>
        </div>

      

ThemesController.php (show method only)

public function show($id)
{
    $theme = Topic::find($id)->theme;
    $topics = Theme::find($id)->topics;

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

      

TopicsController.php (show method only)

public function show($id)
{
    $theme = Theme::find($id);
    $topic = Topic::find($id);

    return view('topics.topic')->with('theme', $theme)->with('topic', $topic);

}

      

Thanks for looking at my code. This problem has been sitting here for a long time, and I want to move on. Thank you for your help!

+3


source to share


1 answer


Your controller code just finds a theme with an ID $id

and a theme (singular) with an ID $id

. This particular topic may not appear at all in that particular topic. They probably have nothing to do with each other.

To find topics related to a topic with an ID $id

, you must do this:

$theme = Theme::find($id)->with('topics');

      

(this assumes your model relationships are set up correctly, you are not showing them to us). See docs eagerly download .

To access the themes in your view, follow these steps:



@foreach ($theme->topics as $topic)
    ...
    {{ $topic->user->username }}
    ...

      

When developing, you can simply

return $theme;

      

in your controller to see the data structure so you can decide how to process and iterate over it.

0


source







All Articles