承接 kblais/laravel-helpers 相关项目开发

从需求分析到上线部署,全程专人跟进,保证项目质量与交付效率

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

kblais/laravel-helpers

Composer 安装命令:

composer require kblais/laravel-helpers

包简介

A collection of helpers for your Laravel application.

README 文档

README

Laravel-helpers is a collection of helpers for your Laravel application.

Installation

Require this package with Composer :

composer require kblais/laravel-helpers

List of helpers

SingularTableNameTrait

Use a singular table name instead of default plural table name.

Usage

Add the trait in your model :

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;
use Kblais\LaravelHelpers\Eloquent\SingularTableNameTrait;

class User extends Model
{
    use SingularTableNameTrait;
}

OrderByDefaultOrderTrait and OrderByDefaultOrderInterface

A global scope to apply a default order on your Eloquent model, and a trait you can use to define your default order directly in your model attributes.

Usage

namespace App;

use Illuminate\Database\Eloquent\Model;
use Kblais\LaravelHelpers\Eloquent\OrderByDefaultOrderTrait;
use Kblais\LaravelHelpers\Eloquent\OrderByDefaultOrderInterface;

class User extends Model implements OrderByDefaultOrderInterface
{
    use OrderByDefaultOrderTrait;

    /**
     * Defaults to :
     * - column: `created_at`
     * - asc: `true`
     */
    protected $defaultOrder = [
        'column' => 'last_login_at',
        'asc' => 'false',
    ];
}

RelationshipHelpers

A list of helpers for your Eloquent relations.

syncHasManyRelation

Synchronize a hasMany relation, deleting old items, updating existing and creating new ones.

Usage

Let's start from this model:

namespace App;

use App\Cat;
use Illuminate\Database\Eloquent\Model;
use Kblais\LaravelHelpers\Eloquent\RelationshipHelpersTrait;

class User extends Model
{
    use RelationshipHelpersTrait;

    protected $fillable = [
        'name', 'email', 'cats',
    ];

    protected static function boot()
    {
        parent::boot();

        self::saved(function ($user) {
            $user->syncHasManyRelation('cats');
        });
    }

    public function cats()
    {
        return $this->hasMany(Cat::class);
    }

    public function setCatsAttribute($cats)
    {
        $this->setHasManyItems('cats', $cats);
    }
}

Based on this, you can directly add cats to you user like that:

$user->create([
    'name' => 'John Doe',
    'email' => 'john.doe@example.com',
    'cats' => [
        [
            'name' => 'Garfield',
            'color' => 'orange',
        ],
        [
            'number' => 'Fuzzy',
            'color' => 'yellow',
        ],
    ],
]);

When you update your model, if you pass a cats key, cats will automatically be created if not existing, updated, or deleted if not in your cats array.

syncHasOneRelation

Synchronize a hasOne relation, creating the new relation item or updating it.

Usage
namespace App;

use App\Cat;
use Illuminate\Database\Eloquent\Model;
use Kblais\LaravelHelpers\Eloquent\RelationshipHelpersTrait;

class User extends Model
{
    use RelationshipHelpersTrait;

    protected $fillable = [
        'name', 'email', 'address',
    ];

    protected static function boot()
    {
        parent::boot();

        self::saved(function ($user) {
            $user->syncHasOneRelation('address');
        });
    }

    public function address()
    {
        return $this->hasOne(Address::class);
    }

    public function setAddressAttribute($address)
    {
        $this->setHasOneItem('address', $address);
    }
}

To create your user with its address, you just need the following:

$user->create([
    'name' => 'John Doe',
    'email' => 'john.doe@example.com',
    'address' = [
        'number' => '18',
        'street' => 'rue Scribe',
        'city' => 'Nantes',
        'country' => 'France',
    ],
]);

Passing an address array in your update() method will also update your user's address.

Routing\Middleware\AreRelated

The AreRelated middleware allows you to check if two route resources are related. It currently only works with HasOneOrMany/BelongsTo relations.

Usage

In your app/Http/Kernel.php, add the following line in the $routeMiddleware array:

'areRelated' => \Kblais\LaravelHelpers\Routing\Middleware\AreRelated::class,

Then, let's imagine we have two models Channel and Message:

use \Illuminate\Database\Model;

class Channel extends Model
{
    //
}

class Message extends Model
{
    public function channel()
    {
        return $this->belongsTo(Channel::class);
    }
}

And, in your routes:

Route::resource('channel.message', 'MessageController');

Because your resources and your relations have the same name (channel and message), you can add the middleware to your resource route to assure that the message you try to access belongs to it's channel:

Route::resource('channel.message', 'MessageController')
    ->middleware('areRelated:channel,message');

If you use custom route bindings, the middleware accepts a third attribute to define the relationship name. For example, if these bindings are defined:

Route::bind('discussion', function ($value) {
    return Channel::find($value);
});

Your route definition will be:

Route::resource('discussion.message', 'MessageController')
    ->middleware('areRelated:discussion,message,channel');

Contributing

Guidelines

  • This projects follows the PSR-2 coding standard, ensure the code you write does too.
  • Consider writing tests when adding a new feature.

Running tests

You can run the tests using the following command (be sure to composer install before):

composer run-script test

kblais/laravel-helpers 适用场景与选型建议

kblais/laravel-helpers 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 22.44k 次下载、GitHub Stars 达 3, 最近一次更新时间为 2019 年 07 月 05 日, 在 PHP 生态内属于活跃度较高的组件。

我们在过去多个企业项目中使用过 kblais/laravel-helpers 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 kblais/laravel-helpers 我们能提供哪些服务?
定制开发 / 二次开发

基于 kblais/laravel-helpers 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 22.44k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 3
  • 点击次数: 1
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 3
  • Watchers: 0
  • Forks: 1
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2019-07-05