Laravel 6 Follow-Unfollow system sql problem

111 views Asked by At

I'm new to Laravel and I am trying to do a follow-unfollow system. I have tried to figure it out by myself for a week but I haven't found any solution. I think I am doing wrong the SQL statement in the controller. This is the code in HomeController.php

    public function index()
    {
        $users = Users::where('id', '!=', auth()->id())->take(3)->get();
        $tweets = DB::select("select avatar, name, username, content, hashtag, tweets.updated_at from users join tweets on users.id = tweets.id order by tweets.updated_at DESC");
        $topics = DB::select("select distinct topic from tweets");
        $follow = DB::select("select users.id, following.id name, avatar, username, following.followed_id from following join users on following.id = users.id");

        return view('home', ['users' => $users], array('user' => Auth::user()))->with(['tweets' => $tweets])->with(['topics' => $topics])->with(['follow' => $follow]);
    }

And this is the code in home.blade.php

                    @foreach($follow as $user)
                    <div class="backgroundColor">
                        <div class="container">

                            <br>
                            <div class="row">
                                <div class="col-3">
                                    <img class="avatar" src="images/uploads/avatars/{{ $user->avatar }}" alt="pic" width="32px">

                                </div>
                                <div class="col-5">

                                    <a href="{{ route('visit') }}">
                                        <h6 class="effect">{{ $user->name }}</h6>
                                    </a>
                                    <h6>{{ $user->username }}</h6>
                                </div>

                                <div class="col-4">
                                    @if($user->id == $user->followed_id)
                                    <a id="followBtn" class="btn btn-info" href="">Following</a>

                                    @else
                                    <a id="followBtn" class="btn btn-info" href="">Follow</a>
                                    @endif
                                </div>
                                <p style="color: white;">{{ $user->followed_id }}</p>
                            </div>
                        </div>
                    </div>
                    <div style="border-bottom: 1px solid gray;"></div>

                    @endforeach

I am also showing the SQL schema Users schema

        Schema::create('following', function (Blueprint $table) {
            $table->bigIncrements('following_id');
            $table->integer('id')->unsigned();
            $table->integer('followed_id');
            $table->timestamps();
        });

I do not have any errors but it does not work at all. I just want to show other users and see if you are following him or not. In home.blade.php I am trying to compare the id with followed_id and if this is true it means that I am following it. Maybe my approach it's completely wrong I don't know.

1

There are 1 answers

0
shaedrich On

Your Users model:

public function following() {
    return $this->hasMany(App\Models\Following::class);
}
public function index()
    {
        $users = Users::where('id', '!=', auth()->id())->with('following')->take(3)->get();
        $tweets = DB::select("select avatar, name, username, content, hashtag, tweets.updated_at from users join tweets on users.id = tweets.id order by tweets.updated_at DESC");
        $topics = DB::select("select distinct topic from tweets");

        return view('home', ['users' => $users], array('user' => Auth::user()))->with(['tweets' => $tweets])->with(['topics' => $topics])->with(['follow' => $follow]);
    }

Inside your home.blade.php

/* ... */
<div class="col-4">
                                    @if($user->following->contains(function($follow) { return $follow->following_id === $user->id; }))
                                    <a id="followBtn" class="btn btn-info" href="">Following</a>

                                    @else
                                    <a id="followBtn" class="btn btn-info" href="">Follow</a>
                                    @endif
                                </div>
/* ... */