zepson/laravel-bookmark 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

zepson/laravel-bookmark

Composer 安装命令:

composer require zepson/laravel-bookmark

包简介

Laravel bookmark

README 文档

README

User bookmark feature for Laravel Application.

CI

Installing

$ composer require trendsoft/laravel-bookmark -vvv

Configuration

This step is optional

$ composer artisan vendor:publish --provider="Trendsoft\\LaravelBookmark\\BookmarkServiceProvider" --tag=config

Migrations

This step is also optional, if you want to custom bookmarks table, you can publish the migration files:

$ composer artisan vendor:publish --provider="Trendsoft\\LaravelBookmark\\BookmarkServiceProvider" --tag=migrations

Usage

Traits

Trendsoft\LaravelBookmark\Traits\Bookmarker

use Illuminate\Database\Eloquent\Model;
use Trendsoft\LaravelBookmark\Traits\Bookmarker;

class User extends Model
{
    use Bookmarker;
}

Trendsoft\LaravelBookmark\Traits\Bookmarkable

use Illuminate\Database\Eloquent\Model;
use Trendsoft\LaravelBookmark\Traits\Bookmarkable;

class Post extends Model
{
    use Bookmarkable;
}

API

$user = User::find(1);
$post = Post::find(1);

$user->bookmark($post);
$user->unBookmark($post);
$user->toggleBookmark($post);

$user->hasBookmarked($post);
$post->isBookmarkedBy($user);

Get user bookmarks with pagination:

$bookmarks = $user->bookmarks()->with('bookmarkable')->paginate(20);

foreach($bookmarks as $bookmark){
    $bookmark->bookmarkable; // App\Post instance
}

Get object bookmarkers:

foreach($post->bookmarkers as $user){
    echo $user->name;
}

with pagination:

$bookmarkers = $post->bookmarkers()->paginate(20);
foreach($bookmarkers as $user){
    echo $user->name;
}

Aggregations

//all
$user->bookmarks()->count();

//with type
$user->bookmarks()->withType(Post::class)->count();

// bookmarkers count
$post->bookmarkers()->count();

List with *_count attribute:

$users = User::withCount('bookmarks')->get();

foreach($users as $user){
    echo $user->bookmarks_count;
}

N +1 issue

To avoid the N+1 issue, you can use eager loading to reduce this operation to just 2 queries. When querying, you may specify which relationships should be eager loaded using the with method:

// Bookmarker
$users = App\User::with('bookmarks')->get();

foreach($users as $user) {
    $user->hasBookmarked($post);
}

// Bookmarkable
$posts = App\Post::with('bookmarks')->get();
// or
$posts = App\Post::with('bookmarkers')->get();

foreach($posts as $post){
    $post->isBookmarkedBy($user);
}

Events

Event Description
Trendsoft\LaravelBookmark\Events\Bookmarked Triggered when the relationship is created.
Trendsoft\LaravelBookmark\Events\Unbookmarked Triggered when the relationship is deleted.

Contributing

You can contribute in one of three ways:

  • File bug reports using the issue tracker.
  • Answer questions or fix bugs on the issue tracker.
  • Contribute new features or update the wiki.

The code contribution process is not very formal. You just need to make sure that you follow the PSR-0, PSR-1, and PSR-2 coding guidelines. Any new code contributions must be accompanied by unit tests where applicable.

License

MIT

统计信息

  • 总下载量: 679
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 6
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 0
  • Forks: 6
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2021-02-20

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固