GithubHelp home page GithubHelp logo

Comments (14)

overtrue avatar overtrue commented on May 31, 2024

这俩方法均支持传入目标类型:

$user->followings(Post::class);
$object->followers(); // 这个返回肯定是用户类型了

from laravel-follow.

 avatar commented on May 31, 2024

@overtrue 这样返回的是object, 然而我不能通过$user->followings(User::class)->name;的方式获取此用户关注的用户名称, 这是不是一个集合, 需要用foreach来遍历?

from laravel-follow.

overtrue avatar overtrue commented on May 31, 2024

$user->followings() 获取用户关注中的人,必然是一个集合啊 😭

from laravel-follow.

 avatar commented on May 31, 2024

@overtrue 那么获取一个用户的粉丝和关注的人的数量用count来计算对吧

from laravel-follow.

overtrue avatar overtrue commented on May 31, 2024

from laravel-follow.

kenfu avatar kenfu commented on May 31, 2024

很奇怪,为什么用 @foreach无法把$user->followings(Post::class);列出来?

from laravel-follow.

overtrue avatar overtrue commented on May 31, 2024

@kenfu 结果是?有报错?

from laravel-follow.

kenfu avatar kenfu commented on May 31, 2024

没有报错。
我是用的两个模型,一个是POST,一个VIDEO,此时followables表里有3个App\Models\Topic和2个App\Models\Video的like数据。
$user->likes()->count()前端显示的是0
$user->likes(Topic::class)->count()前端显示3正常。

$videos = $user->likes(Video::class)到前端
但是用@foreach($videos as $video )时,无法取出值来,前端也没有报错。

from laravel-follow.

kenfu avatar kenfu commented on May 31, 2024

大神再不修改,我真的是要自己造轮子了,但是不甘啊! 请问下,你的多态多对多关系是如何实现toggleLike的,这个厉害。

from laravel-follow.

overtrue avatar overtrue commented on May 31, 2024

调试为啥要看前端有没有啊?你不应该是 dd($videos) 就能知道结果么?

from laravel-follow.

kenfu avatar kenfu commented on May 31, 2024

这样的代码我看不懂,所以就笨方法了。

MorphToMany {#326 ▼
#morphType: "followable_type"
#morphClass: "App\User"
#inverse: true
#table: "followables"
#foreignKey: "user_id"
#relatedKey: "followable_id"
#relationName: "likes"
#pivotColumns: array:3 [▶]
#pivotWheres: array:1 [▶]
#pivotWhereIns: []
#pivotCreatedAt: null
#pivotUpdatedAt: null
#using: null
#query: Builder {#341 ▶}
#parent: User {#343 ▶}
#related: User {#337 ▶}
}

from laravel-follow.

overtrue avatar overtrue commented on May 31, 2024

哈哈,我知道问题所在了:试试 $user->likes(Video::class)->get()

from laravel-follow.

kenfu avatar kenfu commented on May 31, 2024

感谢大神!终于没放弃! 这个在后端好用,能否改进下源码,让我直接在前端 $user->likes(topic)就可以选择我需要的东西。如果这个在后台,感觉传参数很麻烦,而且 Video::class 还需要在上面use App\Models\Video;对吗?

$user->likes()列出我所有喜欢的,$user->likes('topic')列出沃Topic下的喜欢。

from laravel-follow.

overtrue avatar overtrue commented on May 31, 2024

$user->likes()列出我所有喜欢的,$user->likes('topic')列出沃Topic下的喜欢

不行的,万一你要用它来查询就无解了。。。

而且 Video::class 还需要在上面use App\Models\Video;对吗?

是的,因为要支持多类型,所以如果要筛选类型就得自己传类型了

不过这个毕竟是 trait 嘛,所以你可以考虑在你的模型里添加方法:

public function likeVideos() 
{
      return $this->likes(\App\Models\Video::class)->get();
}

然后

$user->likeVideos() 就可以得到全部啦

from laravel-follow.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.