samwrigley / laravel-support
一套用于快速、轻松构建 Laravel 应用的辅助工具
此包的官方仓库似乎已不存在,因此该包已被冻结。
Requires
- php: ^7.2
- illuminate/database: ~5.5.0|~5.6.0|~5.7.0|~5.8.0|^6.0
- illuminate/support: ~5.5.0|~5.6.0|~5.7.0|~5.8.0|^6.0
Requires (Dev)
- orchestra/testbench: ~3.5.0|~3.6.0|~3.7.0|~3.8.0|^4.0
- phpunit/phpunit: ^8.0
- squizlabs/php_codesniffer: ^3.1
This package is auto-updated.
Last update: 2021-10-26 02:01:45 UTC
README
一套用于快速、轻松构建 Laravel 应用的辅助工具。
安装
此包可以通过 Composer 安装
composer require samwrigley/laravel-support
用法
特性
使用此包时,以下特性可用
特性 | 代码 | 描述 |
---|---|---|
CanBePublished |
视图 | 读取 |
HasAuthor |
视图 | 读取 |
HasCategories |
视图 | 读取 |
HasCategory |
视图 | 读取 |
HasPaths |
视图 | 读取 |
CanBePublished
此特性使设置项目为草稿、计划发布或已发布变得简单。对于例如在公开访问之前可能需要经过审查过程的模型(如 Post
或 Article
)非常有用。
- 要使用
CanBePublished
特性,将其添加到模型中,如下所示
use SamWrigley\Support\Traits\CanBePublished; class Article { use CanBePublished; ... }
- 您还需要将
published_at
列添加到相应的表中。例如
Schema::create('article', function (Blueprint $table) { ... $table->dateTimeTz('published_at')->nullable(); ... });
一旦添加,此特性将在模型上提供以下所有方法。
您可以使用以下方法轻松发布、计划或草拟项目
$article->publish() // Publish item $article->publish($dateTime) // Scheduled item to be published at given dateTime $article->draft() // Draft item
您可以使用以下方法轻松检查项目是否已发布、计划发布或为草稿
$article->isPublished() // Check if item is published $article->isScheduled() // Check if item is scheduled to be published $article->isDraft() // Check if item is a draft
此特性还添加了多个局部作用域,以帮助查询模型
$articles->published()->get(); // Get all published items $articles->scheduled()->get(); // Get all scheduled items $articles->draft()->get(); // Get all draft items $articles->month($month)->get(); // Get all items published in a given month $articles->year($year)->get(); // Get all items published in a given year $articles->whereBetween($then, $now)->get(); // Get all items published between the given dateTimes $articles->whereBefore($now)->get(); // Get all items published before the given dateTime $articles->whereAfter($then)->get(); // Get all items published after the given dateTime
当然,如果需要,局部作用域也可以链在一起。例如
$articles->month('January')->year('2018')->get(); // Get all items published in January 2018
HasAuthor
此特性使创建一对一 author
关系变得简单。对于例如由 User
发布项目的模型(如 Article
或 Recipe
)非常有用。
- 要使用
HasAuthor
特性,将其添加到模型中,如下所示
use SamWrigley\Support\Traits\HasAuthor; class Article { use HasAuthor; ... }
- 然后,通过向模型中添加一个
author()
方法来定义作者关系。
一旦添加,此特性将添加辅助方法 withAuthor()
以预加载 author
关系
$article->withAuthor()->get();
HasCategories
此特性使创建多对多 categories
关系变得简单。对于例如可以通过类别组织项目的模型(如 Article
)非常有用。
- 要使用
HasCategories
特性,将其添加到模型中,如下所示
use SamWrigley\Support\Traits\HasCategories; class Article { use HasCategories; ... }
- 然后,通过向模型中添加一个
categories()
方法来定义类别关系。
一旦添加,此特性将添加辅助方法 withCategories()
以预加载 categories
关系
$article->withCategories()->get();
此外,还提供了 addCategories()
和 updateCategories()
辅助方法
$article->addCategories($categories); // Assign given categories to article $article->updateCategories($categories); // Update given categories on article
HasCategory
该特性使得创建一对一的 分类
关系变得简单。对于例如 文章
模型,其中项目可以按单个分类进行组织时非常有用。
- 要使用
HasCategory
特性,请按以下方式将其添加到您的模型中
use SamWrigley\Support\Traits\HasCategory; class Article { use HasCategory; ... }
- 然后,通过在模型中添加一个
category()
方法来定义分类关系。
添加后,该特性会将辅助方法 withCategory()
添加到模型中,以便懒加载 category
关系
$article->withCategory()->get();
此外,还有 addCategory()
和 removeCategory()
辅助方法可用
$article->addCategory($category); // Assign given category to article $article->removeCategory(); // Remove category from article
HasPaths
该特性使得获取项目各种 CRUD 路径变得容易,例如 显示
、更新
或 销毁
。
- 要使用
HasPaths
特性,请将以下内容添加到您的模型中
use SamWrigley\Support\Traits\HasPaths; class Article { use HasPaths; /** * The route namespaces. * * @var array */ protected $namespaces = [ 'web' => 'blog', 'admin' => 'admin.blog', ]; ... }
属性 namespaces
允许您为模型定义默认的路由命名空间。需要 web
和 admin
命名空间,但如果你正在重写默认方法参数,则可以添加额外的命名空间,如下文所述。每个命名空间的值应反映您模型的路由命名空间。
添加后,该特性会将以下方法添加到模型中
$article->createPath(); $article->storePath(); $article->showPath(); $article->editPath(); $article->updatePath(); $article->destroyPath();
每个方法返回相应的完整 CRUD 路径。例如
$article->showPath(); // https://samwrigley.co.uk/blog/example $article->updatePath(); // https://samwrigley.co.uk/admin/blog/1
以下是一个使用示例
<a href="{{ $article->showPath() }}"> {{ $article->title }} <a/>
您也可以通过传递关联数组来覆盖 HasPaths
方法的默认参数。
在以下示例中,我们正在获取文章在管理后台的完整显示路径,而不是在前端。注意,我们还将路由键名称从文章的 slug
更改为 id
$article->showPath([ 'key' => 'id', 'namespace' => 'admin', ]); // https://samwrigley.co.uk/admin/blog/1
作为参数传递的数组与默认参数合并;因此,您只需定义要覆盖的键/值对。
测试
使用以下命令运行测试
vendor/bin/phpunit
变更日志
有关最近更改的更多信息,请参阅 变更日志。
许可
MIT 许可证 (MIT)。有关更多信息,请参阅 许可文件。