【问题标题】:Laravel 6 Follow-Unfollow system sql problemLaravel 6 Follow-Unfollow 系统sql问题
【发布时间】:2021-08-13 14:08:57
【问题描述】:

我是 Laravel 的新手,我正在尝试做一个关注取消关注系统。我试图自己解决一个星期,但我没有找到任何解决方案。我认为我在控制器中的 SQL 语句做错了。这是 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]);
    }

这是 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

我还展示了 SQL 架构

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

我没有任何错误,但它根本不起作用。我只是想向其他用户展示,看看你是否关注他。在 home.blade.php 中,我试图将 id 与 follow_id 进行比较,如果这是真的,则意味着我正在关注它。也许我的方法完全错误我不知道。

【问题讨论】:

  • “它根本不起作用”是什么意思?
  • 有什么原因,您将 Eloquent 用于 Users 而不是 Tweets 和 Topics?
  • @shaedrich 它没有显示任何用户。如果我在 foreach 中使用 $users 作为 $user 那么它将显示用户,但如果我关注或不关注用户则不会显示
  • 你正在加入following.id = users.id。那是行不通的。您需要加入 following_idfollowed_id

标签: sql laravel laravel-6 laravel-6.2 laravel-controller


【解决方案1】:

您的Users 型号:

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

在你的 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>
/* ... */

【讨论】:

    猜你喜欢
    • 2013-04-29
    • 1970-01-01
    • 1970-01-01
    • 2021-03-30
    • 2021-01-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多