miladimos / laravel-social
一个完整且简单的社交网络工具包
0.6.3
2024-03-16 16:04 UTC
Requires
- php: >=7.4|^8.0
This package is auto-updated.
Last update: 2024-08-28 22:53:56 UTC
README
Laravel 社交包
社交网络的工具包
安装
- 运行以下命令以添加此包
composer require miladimos/laravel-social
- 打开您的 config/socials.php 并将以下内容添加到 providers 数组中
Miladimos\Social\Providers\SocialServiceProvider::class,
- 运行以下命令以安装包
php artisan social:install
- 运行以下命令以迁移数据库
php artisan migrate
功能
标签
首先将 Taggable
特性添加到您希望拥有标签的模型中
namespace App\Models; use Illuminate\Database\Eloquent\Factories\HasFactory; use Miladimos\Social\Traits\Taggable; use Illuminate\Database\Eloquent\Model; class Post extends Model { use HasFactory, Taggable; }
其次您可以使用标签进行操作
namespace App\Http\Controller; use App\Models\Post; use Miladimos\Social\Models\Tag; class YourController extends Controller { public function index() { // first you can create custom tags $tag = Tag::create(['name' => 'tag']); $post = Post::first(); $post->tags; // return attached tags $post->attach($tag); // attach one tag $post->detach($tag); // detach one tag $post->syncTags([$tags]); // sync tags $tag->taggables; // return morph relation to tagged model } }
标签模型具有软删除特性。
点赞
收藏
关注
分类
首先将 Taggable
特性添加到您希望拥有附件的模型中
namespace App\Models; use Illuminate\Database\Eloquent\Factories\HasFactory; use Illuminate\Database\Eloquent\Model; use Miladimos\Social\Traits\Taggable; class Post extends Model { use HasFactory, Taggable; }
方法
在控制器中您有这些方法
namespace App\Http\Controllers; use App\Models\Post; class PostController extends Controller { public function index() { $post = Post::find(1); $post->likes // return all likes } }
功能
点赞
收藏
收藏
关注/取消关注
评论
$post = Post::find(1);
$post->comment('这是一条评论');
$post->commentAsUser($user, '这是别人的评论'); $comment = $post->comments->first();
$comment->approve();
自动批准评论实现了 Commentator 的 needsCommentApproval 为 false
// 获取所有评论 $comments = $post->comments;
// 获取仅批准的评论 $approved = $post->comments()->approved()->get();
投票/评分系统