Comments (14)
这俩方法均支持传入目标类型:
$user->followings(Post::class);
$object->followers(); // 这个返回肯定是用户类型了
from laravel-follow.
@overtrue 这样返回的是object, 然而我不能通过$user->followings(User::class)->name;
的方式获取此用户关注的用户名称, 这是不是一个集合, 需要用foreach来遍历?
from laravel-follow.
$user->followings()
获取用户关注中的人,必然是一个集合啊 😭
from laravel-follow.
@overtrue 那么获取一个用户的粉丝和关注的人的数量用count来计算对吧
from laravel-follow.
嗯
from laravel-follow.
很奇怪,为什么用 @foreach无法把$user->followings(Post::class);列出来?
from laravel-follow.
@kenfu 结果是?有报错?
from laravel-follow.
没有报错。
我是用的两个模型,一个是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.
大神再不修改,我真的是要自己造轮子了,但是不甘啊! 请问下,你的多态多对多关系是如何实现toggleLike的,这个厉害。
from laravel-follow.
调试为啥要看前端有没有啊?你不应该是 dd($videos)
就能知道结果么?
from laravel-follow.
这样的代码我看不懂,所以就笨方法了。
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.
哈哈,我知道问题所在了:试试 $user->likes(Video::class)->get()
from laravel-follow.
感谢大神!终于没放弃! 这个在后端好用,能否改进下源码,让我直接在前端 $user->likes(topic)就可以选择我需要的东西。如果这个在后台,感觉传参数很麻烦,而且 Video::class 还需要在上面use App\Models\Video;对吗?
$user->likes()列出我所有喜欢的,$user->likes('topic')列出沃Topic下的喜欢。
from laravel-follow.
$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)
- Error Traits HOT 9
- Get List of Followings users HOT 1
- config key typo HOT 2
- Question: How to send notification/email to user when they have a new follower? HOT 11
- get General error: 1 no such table: followables error HOT 12
- $user->followings not working properly HOT 6
- how to recover data from the "following" user? HOT 2
- Feature Request: Followers you know HOT 9
- Extending the model
- Trait "Overtrue\LaravelFollow\Traits\Followable" not found HOT 2
- Update for Laravel 10
- [ERROR] After running command - SSL Issues HOT 3
- php artisan vendor:publish does not publish the migration or config HOT 3
- Error after upgrade to 5.1.0 HOT 14
- wrong sample code in readme.
- Can't Get Following Model Data HOT 1
- getting "general error" when attempting to use UUIDs HOT 2
- sorry. please delete this issue
- has_followed没有实现 HOT 2
- Unable to Handle UUIDs HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from laravel-follow.