简体   繁体   English

Laravel 从 controller 在视图中获得 class

[英]Laravel getting class on view without passing from controller

I have a pivot table post_profil e that contains the hearts (likes) of each post.我有一个 pivot 表post_profil e,其中包含每个帖子的心(喜欢)。

Post.php Post.php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Post extends Model
{
    protected $guarded = [];

    public function user()
    {
       return $this->belongsTo(User::class);
    }

    public function profilesHearted()
    {
        return $this->belongsToMany(Profile::class);
    }
}

Profile.php简介.php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Profile extends Model
{
    protected $guarded = [];
    
    public function profileImage()
    {
        $imagePath = ($this->image) ? $this->image : 'profile/czyhBQu2YWX6gvBivZrnEs2ORoBwr3d9mzkwxk8k.png';
        return $imagePath; 
    }

    public function followers()
    {
        return $this->belongsToMany(User::class);
    }

    public function heartedPosts()
    {
        return $this->belongsToMany(Post::class);
    }

    public function user()
    {
        return $this->belongsTo(User::class);
    }
}

The main view which shows all posts of followed users is working along with the heart button but underneath I want to show liked by <x> and 30 others , where x is first follower of the user if any of the user's follower hearted the post.显示关注用户的所有帖子的主视图与心形按钮一起工作,但在下面我想显示<x> 和其他 30 个喜欢的人,其中x是用户的第一个关注者,如果用户的任何关注者关注该帖子。 I tried many ways to get x but I am a bit lost.我尝试了很多方法来获得x ,但我有点迷茫。

This is PostsController.php index function which leads to the view:这是PostsController.php索引 function 导致视图:

public function index()
{
    $users = auth()->user()->following()->pluck('profiles.user_id');
    $posts = Post::whereIn('user_id', $users)->with('user')->latest()->paginate(5);
    return view('posts.index', compact('posts', 'users' ));
}

What I tried to do but deleted eventually after failing is get each post in foreach loop and check if each $post->heartedProfiles(contains($users)) , but this doesn't work this way.我尝试做但在失败后最终删除的是在 foreach 循环中获取每个帖子并检查每个$post->heartedProfiles(contains($users)) ,但这不起作用。 So another way I tried is in my view ie, index.blade.php所以我尝试的另一种方法是在我看来,即index.blade.php

@foreach($users as $userid)
    User::select('id')->where('profile_id', $userid)->first()
        ->profile->profilesHearted->find($post->id)
@endforeach

Which doesn't work because User class cannot be used directly in view without passing.这不起作用,因为User class 不能在没有通过的情况下直接在视图中使用。

I'm sure there is a simple and cleaner way to do this in Laravel.我确信在 Laravel 中有一种简单而干净的方法可以做到这一点。 This is my first time using Laravel and ORM programming so really felt lost.这是我第一次使用 Laravel 和 ORM 编程所以真的感到迷茫。 Took the FreeCodeCamp Instagram clone tutorial and understood the basics.参加了 FreeCodeCamp Instagram 克隆教程并了解了基础知识。 All I need is to give me some idea to get started.我所需要的只是给我一些开始的想法。

When dealing with many to many you need to use the pivot functions, so you would do like so在处理多对多时,您需要使用 pivot 函数,所以您会这样做

Post::profilesHearted()->wherePivotIn('profile_id', $users)->first()

Not checked the code but hopefully it will give you the idea.没有检查代码,但希望它会给你这个想法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM