Rainieren

Experience

4,680

0 Best Reply Awards

  • Member Since 5 Months Ago
  • 31 Lessons Completed
  • 0 Favorites

19th July, 2017

Rainieren started a new conversation Error If The User Is Not Logged In. • 1 month ago

I have a website where I check if a user is equal to a certain username, In this case, the user name of the reply. Basically, it just checks if the user that is logged in is equal to a comment's username If so they can do this. But the problem is is if a user is not signed in. It gives me an error with Trying to get property of non-object. Is there another way to check this so that it works even when the user is not logged in?

This is the IF-statement I talked about, I have multiple of these throughout the view

@if(Auth::user()->username == $reply->user->username)
                                                    <a href="{{ route('deletereply', ['id' => $reply->id]) }}"  class="waves-effect waves-light btn button-xs-danger right"
                                                       onclick="event.preventDefault(); document.getElementById('delete-form{{ $reply->id }}').submit();">Delete
                                                        <form id="delete-form{{ $reply->id }}"
                                                        action="{{ route('deletereply', ['id' => $reply->id]) }}"
                                                        method="POST"
                                                        style="display: none">
                                                        {{ csrf_field() }}
                                                        {{ method_field('DELETE') }}
                                                        </form>
                                                    </a>
                                                <a  class="waves-effect waves-light btn button-xs-neutral right">Edit</a>
                                            @endif

Thanks in advance!

Rainieren left a reply on Error While Updated To Last PHPStorm Version • 1 month ago

That does the exact same thing as I did, It doesn't do anything. Still gives me the same error

Rainieren left a reply on Error While Updated To Last PHPStorm Version • 1 month ago

Same thing, The problem is is in the if statement as i said.

Rainieren left a reply on Error While Updated To Last PHPStorm Version • 1 month ago

Nothing, It's the end of a div.... @Parasoul

Rainieren left a reply on Error While Updated To Last PHPStorm Version • 1 month ago

I'm not logged in but it still should be able to show the page but just without the buttons sinse the user is not logged in.

Rainieren left a reply on Error While Updated To Last PHPStorm Version • 1 month ago

The problem is in the Auth verification. I have multiple IF statements which determine if a user belongs to that post or not by using something like this:

@if(Auth::user()->username == $reply->user->username)

@endif

Before the update, it worked perfectly but now it does not. If I comment all the if statement in my view the site works fine

Rainieren left a reply on Error While Updated To Last PHPStorm Version • 1 month ago

No, Line 41 is just a HTML line which is this <div class="row last-row block-timestamp">. De relations are just fine. I don't get why this happened because i didn't change anything and it worked perfectly 2 days ago

Rainieren started a new conversation Error While Updated To Last PHPStorm Version • 1 month ago

Hello, My website worked just fine, But when I updated my PHPStorm to the last version it suddenly gave me an error. The error states as followed Trying to get property of non-object (View: C:\xampp\htdocs\laravel\forum\resources\views\topics\topic.blade.php)

I don't know what it is because before the update everything worked perfectly. Also, the button with the class submit is now grey? Maybe that has something to do with it?

Here is Topic.blade.php

@extends('layouts.default')

@section('content')
<div class="container first-container">
    <div class="row first-row">
        <div class="col s12">
            <div class="card">
                <div class="card-content clearfix">
                    <span class="card-title">{{ $theme->theme_title }} - {{ $topic->topic_title }}&nbsp;
                        <span class="status-badge status-open">Open</span>
                    </span>
                </div>
            </div>
            <div class="card blue-grey lighten-5">
                <div class="card-content">
                    <div class="collection">
                        <div class="collection-item row">
                            <div class="col s3" style="padding-bottom: 20px;">
                                <div class="avatar collection-link">
                                    <div class="row">
                                        <div class="col s3">
                                            <a href="{{ route('showprofile', ['username' => $topic->user->username ]) }}">
                                                <img src="{{ asset('uploads/avatars/' . $topic->user->avatar) }}" class="circle responsive-img" style="">
                                            </a>
                                        </div>
                                        <div class="col s9">
                                            <p class="user-name">{{ $topic->user->username }}</p>
                                        </div>
                                    </div>
                                    <p>Role: {{ $topic->user->role->role_name }}</p>
                                    <p>Member since: {{ $topic->user->created_at }}</p>
                                    <p class="post-timestamp">Posted: {{$topic->created_at }}</p>
                                </div>
                            </div>
                            <div class="col s9">
                                <div class="row last-row">
                                    <div class="col s12">
                                        <h6 class="title">{{ $topic->topic_title }}</h6>
                                        <p>{!! $topic->topic_text !!}</p>
                                    </div>
                                </div>
                                <div class="row last-row block-timestamp">
                                    <div class="col s6">
                                        <p class="post-timestamp">Last changed: {{ $topic->updated_at->diffForHumans() }}</p>
                                    </div>
                                    <div class="col s6">
                                        @if(Auth::user()->username == $topic->user->username || Auth::user()->role_id == 2)
                                            <p class="post-timestamp right">
                                                <a href="{{ route('deletetopic', ['id' => $topic->id]) }}"
                                                   onclick="event.preventDefault(); document.getElementById('delete-form{{ $topic->id }}').submit();">delete
                                                    <form id="delete-form{{ $topic->id }}"
                                                          action="{{ route('deletetopic', ['id' => $topic->id]) }}"
                                                          method="POST"
                                                          style="display: none">
                                                        {{ csrf_field() }}
                                                        {{ method_field('DELETE') }}
                                                    </form>
                                                </a>
                                            </p>
                                        @endif
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
            </div>

            @if(Auth::guest())

            @else
            <div class="card">
                <div class="card-content">
                    <span class="card-title">Leave a Reply</span>
                    <div class="row">
                        <form method="POST" action="{{ route('createreply') }}">
                            {{ csrf_field() }}
                            <input type="hidden" name="user_id" value="{{ Auth::user()->id }}">
                            <input type="hidden" name="topic_id" value="{{ $topic->id }}">
                            <div class="col s1">
                                <img src="/uploads/avatars/{{ Auth::user()->avatar }}" class="" style="height: 64px;width: 64px;position: relative;top: 15px;">
                            </div>
                            <div class="form-group col s11 input-field comment-form-bubble">
                                <textarea id="message-body textarea1" class="form-control" rows="10" name="reply_text" placeholder="Got something to say?" style="height: 5rem; padding-left: 5px;"></textarea>
                            </div>
                            <div class="col s12 offset-s1">
                                <button class="btn blue right" type="submit"><i class="material-icons right">reply</i>Reply</button>
                            </div>
                        </form>
                    </div>
                </div>
            </div>
            @endif

            <div class="card">
                <div class="card-content">
                    <p>Comments • {{ $topic->replies->count() }}</p>
                    @foreach($topic->replies->sortByDesc('created_at') as $reply)
                    <div class="collection" style="border: none;">
                            <div class="collection-item row" >
                                <div class="col s3" style="padding-bottom: 20px;">
                                    <div href="" class="avatar collection-link">
                                        <div class="row">
                                            <div class="col s3"><a href="{{ route('showprofile', ['username' => $reply->user->username ]) }}">
                                                    <img src="{{ asset('uploads/avatars/' . $reply->user->avatar) }}" alt="" class="" style="width: 50px;">
                                                </a>
                                            </div>
                                            <div class="col s9">
                                                <p class="user-name">{{ $reply->user->username }}</p>
                                            </div>
                                        </div>
                                        <p>Role: {{ $reply->user->role->role_name }}</p>
                                        <p>Posted: {{ $reply->created_at->diffForHumans() }}</p>
                                    </div>
                                </div>
                                <div class="col s9">
                                    <div class="row last-row">
                                        <div class="col s12">
                                            <p>{{ $reply->reply_text }}</p>
                                        </div>
                                    </div>
                                    <div class="row last-row block-timestamp">
                                        <div class="col s6">
                                            <a href=""><small style="font-size: 14px; color: grey">Reply</small></a>
                                        </div>
                                        <div class="col s6">
                                            @if(Auth::user()->username == $reply->user->username)
                                                <small style="font-size: 14px; color: red; margin-left: 10px;" class="right">
                                                    <a href="{{ route('deletereply', ['id' => $reply->id]) }}" style="color: red;"
                                                       onclick="event.preventDefault(); document.getElementById('delete-form{{ $reply->id }}').submit();">Delete
                                                        <form id="delete-form{{ $reply->id }}"
                                                        action="{{ route('deletereply', ['id' => $reply->id]) }}"
                                                        method="POST"
                                                        style="display: none">
                                                        {{ csrf_field() }}
                                                        {{ method_field('DELETE') }}
                                                        </form>
                                                    </a>
                                                </small>
                                                <small style="font-size: 14px; color: cornflowerblue" class="right">Edit</small>
                                            @endif
                                        </div>
                                    </div>
                                </div>
                            </div>
                        </div>
                    @endforeach

                    @if($topic->replies->count() == 0 )
                            <h5 style="font-style: italic; color: grey" >No Replies yet</h5>
                    @endif
                </div>
            </div>
        </div>
    </div>
</div>


@endsection

In the error it says this:

in d2de93f86c5533060cc20907e5fb47a8b04b8bc9.php line 41
at CompilerEngine->handleViewException(object(ErrorException), 1) in PhpEngine.php line 44
at PhpEngine->evaluatePath('C:\\xampp\\htdocs\\laravel\\forum\\storage\\framework\\views/d2de93f86c5533060cc20907e5fb47a8b04b8bc9.php', array('__env' => object(Factory), 'app' => object(Application), 'errors' => object(ViewErrorBag), 'topic' => object(Topic), 'theme' => object(Theme))) in CompilerEngine.php line 59
at CompilerEngine->get('C:\\xampp\\htdocs\\laravel\\forum\\resources\\views/topics/topic.blade.php', array('__env' => object(Factory), 'app' => object(Application), 'errors' => object(ViewErrorBag), 'topic' => object(Topic), 'theme' => object(Theme))) in View.php line 137
at View->getContents() in View.php line 120

Maybe you guys can help me out? Thanks in advance!

13th July, 2017

Rainieren left a reply on PHPstorm Not VueJS Friendly? • 1 month ago

@bashy No, It still gives me a whole lot of errors, By the way, how did you put a photo there?

Rainieren started a new conversation PHPstorm Not VueJS Friendly? • 1 month ago

I have VueJS installed in my file in PHPstorm but it looks like this. http://imgur.com/a/l545K All kind of errors and warnings but it still works. I installed the VueJS plugin for PHPstorm but that doesn't work. Is this normal? This really frustrates me because there are no actual errors

Rainieren left a reply on Laravel Make:auth Call To Undefined Function Get() • 1 month ago

Nevermind i fixed it already

Rainieren started a new conversation Laravel Make:auth Call To Undefined Function Get() • 1 month ago

Hello, I'm following the tutorial "Let's build a forum with laravel" And when I try to do "Php artisan make: auth" i get this error [Symfony\Component\Debug\Exception\FatalThrowableError] Call to undefined function get()

What is causing this? I don't know where the problem is so I don't know what code to post.

Thanks in advance

12th July, 2017

Rainieren started a new conversation Laravel Feedback When Leaving Comment • 1 month ago

Hello, Is there a way so the user gets a better feedback when leaving a comment on something. I have a system right now where if the user leaves a reply, he/she gets redirected to the top of the page and that is not really user-friendly because the user needs to scroll down to view their comment. Is there a way so the user stays or gets directed to his/her comment?

11th July, 2017

Rainieren left a reply on Laravel Delete All With Relation? • 1 month ago

For me, it doesn't because I'm not that familiar with foreign keys? I thought I could just do this in the destroy method using some similar code that I was already using. but I guess that is a no go? I'll try to figure it out if its the only option?

Rainieren left a reply on Laravel Delete All With Relation? • 1 month ago

I looked at that one @rob897 and if I read it correctly he just wants to delete 2 things related to each other, I'm trying to delete 3 things related to each other, A theme, All the topics within that theme and all the replies to all those topics.

Rainieren left a reply on Laravel Delete All With Relation? • 1 month ago

@rob897 I don't quite understand what ur saying. My database is correct. Can you maybe explain or show me what u man?

Rainieren started a new conversation Laravel Delete All With Relation? • 1 month ago

Hello, I'm trying to figure out how I can delete everything that is connected to a theme, Currently, I have a topic which has replies and to delete those 2 from the database I do something like this in the destroy method

$topic = Topic::find($id);

$topic->replies()->delete();

$topic->delete();

return redirect('/');

But a topic is within a theme. So what I'm trying to do is, add a function which allows the user to delete a theme, But that function also needs to delete ALL the topics within that theme and then ALL the replies of all those topics.

I maybe figured that it needs to look something like this but I can be completely wrong.

$theme = Theme::find($id);
$theme->topics()->replies->delete();
$theme->delete();

return redirect('/')

The relations between all the things are okay but I can list them here, A theme has many Topics A Topic has many replies

How do I do this? Thanks in advance

PS: Sorry for the weird title, I don't know how to explain this (Tips always appreciated)

10th July, 2017

Rainieren started a new conversation Laravel Submitting A Form By Pressing Enter • 1 month ago

I made a couple of forms which allowed the user to press a button and the form gets submitted. Is there a way to submit a form by pressing the Enter key? I currently have a simple form which is this.

<div class="card">
                <div class="card-content">
                    <span class="card-title">Leave a Reply</span>
                    <div class="row">
                        <form method="POST" action="{{ route('createreply') }}">
                            {{ csrf_field() }}
                            <input type="hidden" name="user_id" value="{{ Auth::user()->id }}">
                            <input type="hidden" name="topic_id" value="{{ $topic->id }}">
                            <div class="form-group col s12 input-field">
                                <i class="material-icons prefix">mode_edit</i>
                                <textarea id="message-body textarea1" class="form-control materialize-textarea" name="reply_text" placeholder="Type your reply"></textarea>
                            </div>
                            <div class="col s12">
                                <button class="btn right blue-grey darken-4" type="submit"><i class="material-icons right">reply</i>Reply</button>
                            </div>
                        </form>
                    </div>
                </div>
            </div>

Is this possible to do?

Rainieren started a new conversation Call To Undefined Relationship [topic] On Model [App\Theme]. • 1 month ago

I'm trying to display the latest Topic that is related to a Theme. I did this before and I tried the following code in the ThemesController (Index method, Sinse it's the homepage),

$topics = Theme::with('topic', 'lastTopic.user')->get();
$themes = Theme::all();

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

And the method I'm calling is this.

public function lastTopic()
    {
        return $this->hasOne(Topic::class)->latest();
    }

The method sits inside the Theme Model. So what is causing this issue? This is for my homepage so the route is this Route::get('/', [email protected]')->name('home');. I know it is something I've done before but I can't figure it out. Thanks in advance

7th July, 2017

Rainieren left a reply on Laravel Get Latest Reply From Topic • 1 month ago

But @Demers94 The problem is now If a topic has no replies it gives me Trying to get property of non-object Which makes sense to me since there is not yet a reply left. Can I do something with an if statement that detects if a comment has been left, show the latest one? If not, Show some other text?

Rainieren started a new conversation Laravel Get Latest Reply From Topic • 1 month ago

Hello, I'm trying to display the latest post's username and date. I used something similar by using. Something along the lines of {{ topic->replies->sortBydesc('id')->first->user->username}} It makes sense to me. I also used something like $Allreplies which I defined in the controller with $allposts = Reply::all(); but that gets me ALL the replies of the entire forum and that's not what I want, It needs to display the latest reply of that particular topic.

Can someone tell me how to do this? My Controller looks like this

public function show($id)
    {

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

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

21st June, 2017

Rainieren left a reply on Call To Undefined Method Intervention\Image\File::delete() • 1 month ago

My Controller as it is now, It looks like this.

public function update_avatar(Request $request) {

        $this->middleware('auth');

        if ($request->hasFile('avatar')) {
            $avatar = $request->file('avatar');
            $filename = Auth::user()->username . time() . '.' . $avatar->getClientOriginalExtension();
            Image::make($avatar)->fit(300,300)->save( public_path('/uploads/avatars/' . $filename));

            $user = Auth::user();
            $user->avatar = $filename;
            $user->save();

            //Verwijderd vorige foto
            if ($user->avatar != 'default.jpg') {
                $path = public_path('uploads'.DIRECTORY_SEPARATOR.'avatars'. DIRECTORY_SEPARATOR.Auth::user()->Avatarpath);
                if (file_exists($path)) {
                    Storage::delete($path);
                }
            }
        }

        return view('user.profile', array('user' => Auth::user()));
        //Image intervention
    }

I want to delete their previous photo because if everybody uploads a photo and their picture doesn't get deleted, my folder fills up with thousands of pictures and takes in ALOT of space. So if I can find a way to delete them, It has a big advantage.

Rainieren left a reply on Call To Undefined Method Intervention\Image\File::delete() • 1 month ago

Sorry, I have the same method for a banner also. Just change the name banner to avatar, and it will make sense.

The logic behind the delete is IF the new file is not equal to the previous file, Delete the previous file. Is that even possible to do? Because if u use the login that you said, "If the image is called something other then default.jpg then delete it". Simply says delete everything if it's not called default.jpg. If I'm correct.

Rainieren left a reply on Call To Undefined Method Intervention\Image\File::delete() • 1 month ago

There was no use statement Because I just found out that Intervention does not have a Delete method. So I tried to use Laravel's Storage delete method, Like this.

if ($user->avatar != 'default.jpg') {
                $path = public_path('uploads'.DIRECTORY_SEPARATOR.'avatars'. DIRECTORY_SEPARATOR.Auth::user()->Avatarpath);
                if (file_exists($path)) {
                    Storage::delete($path);
                }
            }

But this looks like it skips the whole if statement. Because it redirects me to the profile page like it should and changes the profile picture, but it doesn't delete the previous file.

Rainieren started a new conversation Laravel Giving A Folder Acces • 1 month ago

In laravel I have a delete method. A user has a profile picture and if the user updates their profile picture their old one gets deleted. But the delete method doesn't work and I think it has something to do with the permissions of the folder. It looks like it is not allowed to delete a file. Does someone know or is someone familiar with this problem? Thanks for your help.

This is the code I'm trying to execute. Which is giving me the error. Look at the last IF statement

public function update_banner(Request $request) {

        if ($request->hasFile('banner')) {
            $banner = $request->file('banner');
            $filename = Auth::user()->username . time() . '.' . $banner->getClientOriginalExtension();
            Image::make($banner)->fit(1050,250)->save( public_path('/uploads/banners/' . $filename));

            $user = Auth::user();
            $user->banner = $filename;
            $user->save();

    
            if ($user->banner!= 'defaultbanner.jpg') {
                $path = 'uploads/banners/';
                $lastpath = Auth::user()->Bannerpath;
                File::delete(public_path($path . $lastpath));
            }
        }

        return view('user.profile', array('user' => Auth::user()));
        //Image intervention
    }

Thanks in advance!

Rainieren started a new conversation Call To Undefined Method Intervention\Image\File::delete() • 1 month ago

I made a method which allows the user to update their profile picture. I also added a function which deletes their old profile picture if they update to a new one. But i get an error saying Call to undefined method Intervention\Image\File::delete().

What is causing this? In the code below it makes perfect sense for me. I hope you guys can help. Thanks in advance

Code: UserController.php

    public function update_avatar(Request $request) {

        $this->middleware('auth');

        if ($request->hasFile('avatar')) {
            $avatar = $request->file('avatar');
            $filename = Auth::user()->username . time() . '.' . $avatar->getClientOriginalExtension();
            Image::make($avatar)->fit(300,300)->save( public_path('/uploads/avatars/' . $filename));

            $user = Auth::user();
            $user->avatar = $filename;
            $user->save();

            //Verwijderd vorige foto
            if ($user->avatar != 'default.jpg') {
                $path = 'uploads/avatars/';
                $lastpath = Auth::user()->Avatarpath;
                File::delete(public_path($path . $lastpath));
            }
        }

        return view('user.profile', array('user' => Auth::user()));

Route: `Route::post('{username}/profile', [email protected]_avatar');`

The form that is supposed to do this.

    <div class="row">
            <div class="col l12 m12 s12">
                <div class="card">
                    <div class="card-content">
                        <form enctype="multipart/form-data" action="profile" method="POST">
                            <input type="file" name="avatar">
                            <input type="hidden" name="_token" value="{{ csrf_token() }}">
                            <input type="submit" class="pull-right btn btn-primary" value="Change profile">
                        </form>
                    </div>
                </div>
            </div>
        </div>

Changing the picture works just fine. If I reload the page the profile picture is updated but it doesn't delete the old one.

19th June, 2017

Rainieren left a reply on Error While Simply Trying To Display A View • 2 months ago

The route is inside a middleware yes, The middleware checks if the user is an Admin or not. If so then he can use the url.

Rainieren started a new conversation Error While Simply Trying To Display A View • 2 months ago

I created a view which allows the user to create a theme/post. The link to that view is in my navbar. But the problem is When I try to click the link (display the view) I get redirected to the home page without any errors whatsoever.

In the link of the navbar, i put <li><a href="{{ route('createtheme') }}"> which simply activates the route with the name "createtheme". This route is as followed: Route::get('/theme/create', [email protected]')->name('createtheme');. So this activated the create method in the ThemesController.php. Which is:

public function create()
    {
        return view('themes.create');
    }

So if I read this correctly, This is supposed to give me the right view, right? And it doesn't give me any errors so I don't know where to look.

This is the view I'm trying to display:

    @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">
                        <span class="card-title">New theme</span>
                    </div>
                </div>
                <div class="card">
                    <div class="card-content">
                        <form method="POST" action="">
                            {{ csrf_field() }}
                            <input type="hidden" name="user_id" value="{{ Auth::user()->id }}">
                            <div class="row">
                                <div class="input-field col s6 has-error form-group">
                                    <input id="title" class="form-control" type="text" name="title" placeholder="Title of topic">
                                    <label for="title" class="active">Title of theme</label><span>Titel is mandatory!</span>
                                </div>
                                <div class="file-field input-field col s6 form-group">
                                    <div class="btn cyan darken-1 disabled"><span>Attachment</span>
                                        <input id="attachement" type="file" name="attachment" class="disabled">
                                    </div>
                                    <div class="file-path-wrapper form-group">
                                        <input type="text" placeholder="geen" class="file-path validate form-control">
                                    </div>
                                </div>
                            </div>
                            <div class="row">
                                <div class="col s12 form-group">
                                    <textarea id="message-body" class="form-control" name="body"></textarea>
                                </div>
                            </div>
                            <div class="row">
                                <div class="col s6">
                                    Hier komen opties om het onderwerp te sluiten of
                                    aan te geven dat het opgelost is, alleen bij bewerken
                                </div>
                                <div class="col s6">
                                    <a href="" class="btn right cyan darken-1" type="submit">Save</a>
                                </div>
                            </div>
                        </form>
                    </div>
                </div>
            </div>
        </div>
    </div>
    @endsection

    @section('scripts')
    <script type="text/javascript" src="{{ asset('js/ckeditor/ckeditor.js') }}">
    </script>
    <script type="text/javascript" src="{{ asset('js/editor.js') }}"></script>
    <script type="text/javascript" src="{{ asset('js/blog.js') }}"></script>
    <script type="text/javascript" src="{{ asset('js/materialize.js') }}">
    </script>
    @endsection

And here are the routes that are related to this problem.


    Route::get('/theme/create', [email protected]')->name('createtheme');
    Route::post('/theme/create', [email protected]');

And of course the Store method:

    public function store(Request $request)
    {
        Theme::create($request->input()); //Dit doet hetzelfde als bovenstaande

        return redirect('/');
    }

Thanks in advance

18th June, 2017

Rainieren left a reply on Laravel Not Displaying Correct Topic From Database • 2 months ago

@Snapey Okay, i understand it now. Sorry for asking you so much but I was just not thinking straight. Thanks for your help! It works and I got it now! thanks!

Rainieren left a reply on Laravel Not Displaying Correct Topic From Database • 2 months ago

@Snapey You told me to.... What am I supposed to put there then.

This is literally what you told me to do. in your post with the Scenarios.

Rainieren left a reply on Laravel Not Displaying Correct Topic From Database • 2 months ago

Now it shows the correct URL (theme/1/topics/19). but not the correct topic. Now it shows the topic with theme_id 9 and topic_id 1. Instead of the topic with theme_id 1 and topic_id 19. The link looks like this now. As you said. And it works and makes sense! {{ route('showtopic', ['theme_id'=>$theme->id, 'topic_id' => $topic->id]) }}

And my show method looks like this.

public function show($topic_id)
    {

        $topic = Topic::with('theme')->findOrFail($topic_id);

        return view('topics.topic')->withTopic($topic)->withTheme($topic->theme);

    }

I think there is something messed up between the 2 and it is something I can't see otherwise I could've fixed it myself.

Rainieren left a reply on Laravel Not Displaying Correct Topic From Database • 2 months ago

@Snapey There is one small problem now. The url is switched up now. Its shows the topic of ffeeney which is correct. but the URL is theme/19/topics/1 instead of theme/1/topics/19. What is causing this?

Rainieren left a reply on Laravel Not Displaying Correct Topic From Database • 2 months ago

I got it working, Thanks. I figured it out! Thank you very much!

Rainieren left a reply on Laravel Not Displaying Correct Topic From Database • 2 months ago

I tried your way but it gave me an error as I said,

If I did it correctly. My theme and Controller looks like this if I try your way. theme.blade.php( the link)

<a href="{{ route('showtopic', $topic->id ) }}">

I changed the topic to showtopic because route 'topic' does not exist in web.php. And this is what my TopicsController looks like

public function show($topic_id)
    {

        $topic = Topic::with('theme')->findOrFail($topic_id);

        return view('topics.topic')->withTheme($topic->theme)->withTopic($topic);

    }

This is everything that you said in Scenario #2. Correct it if I'm wrong. But this gives me the error Missing required parameters for [Route: showtopic] [URI: theme/{theme_id}/topics/{topic_id}].

If I read the code this should just work and give me the correct topic (The one with theme_id 1 and topic_id 19). But it gives me the error above.

Rainieren left a reply on Laravel Not Displaying Correct Topic From Database • 2 months ago

@Snapey This is supposed to bring the user to the single topic. Route::get('/theme/{theme_id}/topics/{topic_id}', [email protected]')->name('showtopic');

There is nothing with a theme in there except the Theme_id. but that is necessary for the URL to work right? The URL is supposed to be like.

If the user clicks on a theme with the theme_id 1. the URL is forum.dev/theme/1/topics. So far so good. The user gets an overview of every topic with the theme_id 1. If the user clicks on a topic in that overview with the (in this case) topic_id 19 the URL is supposed te be like forum.dev/theme/1/topics/19. This link works just fine no problems there. But the post doesn't show the one with topic_id 19 but instead, shows a topic with ID 1. That's the problem here.

In my case the THEME I clicked on has theme_id 1. Every topic with theme_id 1 gets displayed. So far so good. Now if I click on (In this case) the topic of "ffeeney" with the topic_id of 19. It is supposed to show the topic with the topic_id 19 right? And that's where the problem is. Instead of showing topic_id 19. It shows topic_id 1 and topic_id 1 has a theme_id of 9. Which does not make sense. I uploaded a 30-second video on youtube which visualises the problem. https://youtu.be/7UpabVE3GKU. I hope this make thing clear.

Rainieren left a reply on Laravel Not Displaying Correct Topic From Database • 2 months ago

@Snapey What do you mean remove theme in my web.php? which route?

Rainieren left a reply on Laravel Not Displaying Correct Topic From Database • 2 months ago

@Snapey I think I don't see this the right way as you do. I get the error Missing required parameters for [Route: showtopic] [URI: theme/{theme_id}/topics/{topic_id}]. (View: C:\xampp\htdocs\laravel\forum\resources\views\themes\theme.blade.php) If I change the link to this: <a href="{{ route('showtopic', ['topic_id' => $topic->id ]) }}". Be aware that the link is in a foreach loop. I dont know if this could affect it. But it looks like this and it works: @foreach($theme->topics as $topic) This is still in the Theme.blade.php.

My TopicsController.php looks like this. I took this from your Scenario #2.

public function show($topic_id)
    {
        $topic = Topic::with('theme')->findOrFail($topic_id); 

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

    }

I know it is something I've done before on a previous project. But there it works just fine. but here it does not. Thanks for helping me by the way

Rainieren left a reply on Laravel Not Displaying Correct Topic From Database • 2 months ago

Snapey, In your reply the case for this problem is Scenario #2, A topic belongs to a theme. BUT a theme can have many topics. The link in the theme.blade.php looks like this <a href="{{ route('showtopic', ['theme_id' => $theme->id, 'topic_id' => $topic->id ]) }}" And my show method looks like this.

public function show($topic_id)
    {
        $topic = Topic::with('theme')->findOrFail($topic_id);

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

    }

But this still gives me the error Illegal offset type'. I think the URL shows just fine. Because it showshttp://forum.dev/theme/1/topics/19Which is equal toforum.dev/theme/{theme_id}/topics/{topic_id}. Because if u look at this picture http://imgur.com/a/ljQPm u can see that the topic (With the cat picture) has ID 19. That's also visible in the database: http://imgur.com/a/oFD0E`.

I honestly don't know how to explain this better since I'm not English but I'm trying. I hope this makes some things clearer.

Rainieren left a reply on Laravel Not Displaying Correct Topic From Database • 2 months ago

Snapey, What I'm trying to do is, is that it needs to show the topic that the user clicked on but it doesn't do that. Instead, it shows a random topic. But it's the view that is supposed to show the topic also need te have the $theme variable so I can get the title of the theme were the topic is in. If u read this carefully it will make sense I hope. I can tell my explanations are not that great because no one every truly understands me. I hope this is a good explanation though

17th June, 2017

Rainieren left a reply on Laravel Not Displaying Correct Topic From Database • 2 months ago

I think i'm not thinking straight or something. But it still does the same It is supposed to show the topic of ffeeney but shows the topic of henry62. I have this in my show method now

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

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

    }

And this is the view Topic.blade.php

@section('content')
<div class="container first-container">
    <div class="row first-row">
        <div class="col s12">
            @if(Auth::check())
            <div class="card">
                <div class="card-content clearfix">
                    <span class="card-title">{{ $theme->theme_title }} - {{ $topic->topic_title }}&nbsp;<span class="status-badge status-open">Open</span></span>
                    <a href="" class="btn blue-grey darken-4">Reply</a>
                </div>
            </div>
            @endif

            <div class="card blue-grey lighten-5">
                <div class="card-content">
                    <div class="collection">
                        <div class="collection-item row">
                            <div class="col s3">
                                <div class="avatar collection-link">
                                    <div class="row">
                                        <div class="col s3"><img src="/uploads/avatars/{{ $topic->user->avatar }}" class="circle responsive-img" style=""></div>
                                        <div class="col s9">
                                            <p class="user-name">{{ $topic->user->username }} @if($topic->user->isAdmin())<i class="material-icons verified">verified_user</i>@elseif($topic->user->isModerator())<i class="material-icons mod">supervisor_account</i>@endif </p>
                                        </div>
                                    </div>
                                    <p>{{ $topic->user->role->role_name }}</p>
                                    <p>Vanaf: {{ $topic->user->created_at }}</p>
                                    <p class="post-timestamp">Posted n: {{$topic->created_at}}</p>
                                </div>
                            </div>
                            <div class="col s9">
                                <div class="row last-row">
                                    <div class="col s12">
                                        <h6 class="title">{{ $topic->topic_title }}</h6>
                                        <p>{!! $topic->topic_text !!}</p>
                                    </div>
                                </div>
                                <div class="row last-row block-timestamp">
                                    <div class="col s6">
                                        <p class="post-timestamp">Last changed: {{ $topic->updated_at }}</p>
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
            <div class="card">
                <div class="card-content">
                    @foreach($topic->replies as $reply)
                    <div class="collection">
                            <div class="collection-item row">
                                <div class="col s3">
                                    <div href="" class="avatar collection-link">
                                        <div class="row">
                                            <div class="col s3"><img src="/uploads/avatars/{{ $reply->user->avatar }}" alt="" class="circle" style="width: 50px;"></div>
                                            <div class="col s9">
                                                <p class="user-name">{{ $reply->user->username }} @if($reply->user->isAdmin())<i class="material-icons verified">verified_user</i>@elseif($reply->user->isModerator())<i class="material-icons mod">supervisor_account</i>@endif</p>
                                            </div>
                                        </div>
                                        <p>{{ $reply->user->role->role_name }}</p>
                                        <p>Vanaf: {{ $reply->user->created_at }}</p>
                                        <p class="post-timestamp">Gepost op: {{ $reply->created_at }}</p>
                                    </div>
                                </div>
                                <div class="col s9">
                                    <div class="row last-row">
                                        <div class="col s12">
                                            <p>{!! $reply->reply_text !!}</p>
                                        </div>
                                    </div>
                                    <div class="row last-row block-timestamp">
                                        <div class="col s6">
                                            <p class="post-timestamp">Laatst aangepast op: {{ $reply->updated_at }}</p>
                                        </div>
                                    </div>
                                </div>
                            </div>
                        </div>
                    @endforeach
                </div>
            </div>
                @if(Auth::check())
                    <div class="card">
                        <div class="card-content clearfix">
                            <a href="" class="btn blue-grey darken-4">Reply</a>
                        </div>
                    </div>
                @endif
        </div>
    </div>
</div>

Rainieren started a new conversation Laravel Not Displaying Correct Topic From Database • 2 months ago

My problem is the following. I'm making a forum and It doesn't show the topic that I clicked on but another topic from the database. It is supposed to show Topic 19 but it shows Topic 1. The URL is correct. but it shows the wrong topic. Also. As you can see in this GIF: http://imgur.com/a/AfmPZ if u look at the picture and the username, it gets clear what's wrong. How can I solve this? Thank you in advance.

Topic.blade.php (Shows the topic)

<div class="container first-container">
    <div class="row first-row">
        <div class="col s12">
            @if(Auth::check())
            <div class="card">
                <div class="card-content clearfix">
                    <span class="card-title">{{ $theme->theme_title }} - {{ $topic->topic_title }}&nbsp;<span class="status-badge status-open">Open</span></span>
                    <a href="" class="btn blue-grey darken-4">Reply</a>
                </div>
            </div>
            @endif

            <div class="card blue-grey lighten-5">
                <div class="card-content">
                    <div class="collection">
                        <div class="collection-item row">
                            <div class="col s3">
                                <div class="avatar collection-link">
                                    <div class="row">
                                        <div class="col s3"><img src="/uploads/avatars/{{ $topic->user->avatar }}" class="circle responsive-img" style="w"></div>
                                        <div class="col s9">
                                            <p class="user-name">{{ $topic->user->username }} @if($topic->user->isAdmin())<i class="material-icons verified">verified_user</i>@elseif($topic->user->isModerator())<i class="material-icons mod">supervisor_account</i>@endif </p>
                                        </div>
                                    </div>
                                    <p>{{ $topic->user->role->role_name }}</p>
                                    <p>Vanaf: {{ $topic->user->created_at }}</p>
                                    <p class="post-timestamp">Posted n: {{$topic->created_at}}</p>
                                </div>
                            </div>
                            <div class="col s9">
                                <div class="row last-row">
                                    <div class="col s12">
                                        <h6 class="title">{{ $topic->topic_title }}</h6>
                                        <p>{!! $topic->topic_text !!}</p>
                                    </div>
                                </div>
                                <div class="row last-row block-timestamp">
                                    <div class="col s6">
                                        <p class="post-timestamp">Last changed: {{ $topic->updated_at }}</p>
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
            <div class="card">
                <div class="card-content">
                    @foreach($topic->replies as $reply)
                    <div class="collection">
                            <div class="collection-item row">
                                <div class="col s3">
                                    <div href="" class="avatar collection-link">
                                        <div class="row">
                                            <div class="col s3"><img src="/uploads/avatars/{{ $reply->user->avatar }}" alt="" class="circle" style="width: 50px;"></div>
                                            <div class="col s9">
                                                <p class="user-name">{{ $reply->user->username }} @if($reply->user->isAdmin())<i class="material-icons verified">verified_user</i>@elseif($reply->user->isModerator())<i class="material-icons mod">supervisor_account</i>@endif</p>
                                            </div>
                                        </div>
                                        <p>{{ $reply->user->role->role_name }}</p>
                                        <p>Vanaf: {{ $reply->user->created_at }}</p>
                                        <p class="post-timestamp">Gepost op: {{ $reply->created_at }}</p>
                                    </div>
                                </div>
                                <div class="col s9">
                                    <div class="row last-row">
                                        <div class="col s12">
                                            <p>{!! $reply->reply_text !!}</p>
                                        </div>
                                    </div>
                                    <div class="row last-row block-timestamp">
                                        <div class="col s6">
                                            <p class="post-timestamp">Laatst aangepast op: {{ $reply->updated_at }}</p>
                                        </div>
                                    </div>
                                </div>
                            </div>
                        </div>
                    @endforeach
                </div>
            </div>
                @if(Auth::check())
                    <div class="card">
                        <div class="card-content clearfix">
                            <a href="" class="btn blue-grey darken-4">Reply</a>
                        </div>
                    </div>
                @endif
        </div>
    </div>
</div>

Theme.blade.php (The view with the link to the topic)

<div class="col s12">
                <div class="card">
                    <div class="card-content"><span class="card-title">{{ $theme->theme_title }} - Topics</span>
                        <div class="collection">
                            @foreach($theme->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 }} on: {{  $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">Naam</p>
                                            <p class="center-align">Tijd</p>
                                        </div>
                                    </div>
                                </a>
                            @endforeach
                        </div>
                    </div>
                </div>
            </div>

Web.php (Routes)

Route::get('/theme/{theme_id}/topics', [email protected]')->name('showtheme');
Route::get('/theme/{theme_id}/topics/{topic_id}', [email protected]')->name('showtopic');

TopicsController.php (Show method that is supposed to show the correct topic)

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

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

    }

Rainieren left a reply on Show Only Topics That Belong To Theme. • 2 months ago

Nevermind problem is solved now. Thank you

Rainieren left a reply on Show Only Topics That Belong To Theme. • 2 months ago

That makes sense, But it doesn't work. Now it says undefined relationship On model topic

Which is

class Topic extends Model
{
    protected $fillable = [
        'topic_title', 'topic_text', 'theme_id', 'user_id',
    ];

    public function user() {
        return $this->belongsTo('App\User');
    }

    public  function theme() {
        return $this->belongsTo('App\Theme');

    }

    public function replies() {
        return $this->hasMany('App\Reply');
    }
}

My theme controller looks like this now:

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

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

And my theme.blade.php

<div class="col s12">
                <div class="card">
                    <div class="card-content"><span class="card-title">{{ $theme->theme_title }} - Topics</span>
                        <div class="collection">
                            @foreach($theme->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 }} on: {{  $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">Naam</p>
                                            <p class="center-align">Tijd</p>
                                        </div>
                                    </div>
                                </a>
                            @endforeach
                        </div>
                    </div>
                </div>
            </div>

Rainieren left a reply on Show Only Topics That Belong To Theme. • 2 months ago

My show method after your comment looks like this:

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

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

It still doesn't work because i said $topics = Topic::all(); If u read the question. I'm trying to make it so that ONLY the topics show that BELONG to the theme.

Oh and my model looks like this.

class Theme extends Model
{
    protected $fillable = [
        'theme_title', 'theme_description', 'user_id',
    ];

    public function user() {
        return $this->belongsTo('App\User');
    }

    public function topics() {
        return $this->hasMany('App\Topic');
    }


}

Rainieren left a reply on Show Only Topics That Belong To Theme. • 2 months ago

The relationsships between the tables are correct that's not the issue. I was just asking How can I make it that it only shows the topics that belong to the theme.

Rainieren started a new conversation Show Only Topics That Belong To Theme. • 2 months ago

Hello, My problem is. When a user clicks on a theme, he/she needs to get redirected to a page with all the topics that belong to that theme. What I have now is that it shows ALL the topics in the database. How can I make it that it only shows the topics that belong to the theme? I know its something that I've done before but I don't quite remember

Here is the code below of all the files. Thanks you in advance ** theme.blade.php (Where its supposed to show all the topics that belong to 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 }} on: {{  $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">Naam</p>
                                            <p class="center-align">Tijd</p>
                                        </div>
                                    </div>
                                </a>
                            @endforeach
                        </div>
                    </div>
                </div>
            </div>

index.blade.php(Where the link to the list of all the topics that belong to that theme is.

<div class="container first-container">
        <div class="row">
            <div class="col s12">
                <div class="card">
                    <div class="card-content"><span class="card-title">Themes</span>
                        <div class="collection">
                            @foreach($themes as $theme)
                            <a href="{{ route('showtheme', ['theme_id' => $theme->id]) }}" class="collection-item avatar collection-link">
                                <img src="{{ asset('img/icon.png') }}" alt="" class="circle">
                                    <div class="row">
                                        <div class="col s8">
                                            <div class="row last-row">
                                                <div class="col s12">
                                                    <span class="card-title">{{ $theme->theme_title }}</span>
                                                    <p>{{ $theme->theme_description }}</p>
                                                </div>
                                            </div>
                                            <div class="row last-row">
                                                <div class="col s12 post-timestamp">Admin: {{ $theme->user->username }}</div>
                                            </div>
                                        </div>
                                            <div class="col s2">
                                                <h6 class="title center-align">Statistieken</h6>
                                                <p class="center-align">{{ $theme->topics->count() }} topic(s)</p>
                                            </div>
                                            <div class="col s2">
                                                <h6 class="title center-align">Laatste topic</h6>
                                                <p class="center-align">Basuds</p>
                                                <p class="center-align">30-12-2015 19:00</p>
                                            </div>
                                    </div>
                                </a>
                            @endforeach
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>

** Web.php (routes) **

Route::get('/', [email protected]')->name('home');
Route::get('/theme/{theme_id}/topics', [email protected]')->name('showtheme');

Route::get('/theme/{theme_id}/topics/{topic_id}', [email protected]')->name('showtopic');

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

    //THEMES

    Route::get('/theme/{theme_id}/edit', [email protected]')->name('edittheme');
    Route::patch('/theme/{theme_id}/edit', [email protected]')->name('updatetheme');

    Route::get('/theme/create', [email protected]')->name('createtheme');
    Route::post('/theme/create', [email protected]')->name('savetheme');

    Route::delete('/theme/{theme_id}/delete', [email protected]')->name('deletetheme');

    //TOPICS

    Route::get('/theme/{theme_id}/topics/{topic_id}/edit', [email protected]')->name('edittopic');
    Route::patch('/theme/{theme_id}/topics/{topic_id}/edit', [email protected]')->name('updatetopic');

    Route::get('/theme/{theme_id}/topics/create', [email protected]')->name('createtopic');
    Route::post('/theme/{theme_id}/topics/create', [email protected]')->name('savetopic');

    Route::delete('/theme/{theme_id}/topics/{topic_id}/delete', [email protected]')->name('deletetopic');



});

TopicsController.php(show method)

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

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

    }

ThemesController.php(show method)

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

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

Rainieren left a reply on Undefined Variable: Theme • 2 months ago

I think the problem is pretty obvious. I think in the show method I need to pass in the $theme. for the link to work. But I also need to have $topic. That's the problem here, isn't it? How do I fix this then?

Rainieren left a reply on Undefined Variable: Theme • 2 months ago

This is the show method in the topicController.php http://imgur.com/a/uSZpU Is this what you're looking for?

Rainieren left a reply on Undefined Variable: Theme • 2 months ago

Edit Your Profile
Update

Want to change your profile photo? We pull from gravatar.com.