te7a-houdini/laravel-applicant
Composer 安装命令:
composer require te7a-houdini/laravel-applicant
包简介
Simple package to allow model applies and receives applications from other models
README 文档
README
Simple package to allow model applies and receives applications from other models.
for example it will allow you to do something like this:
//User model applies for Group model $user->appliesFor($group); //Group model process application from User model $group->processApplicationFrom($user,'accepted');
Installation
You can install the package via composer:
composer require te7a-houdini/laravel-applicant
Then publish the configurations and migrations:
php artisan vendor:publish --provider="Te7aHoudini\LaravelApplicant\LaravelApplicantServiceProvider"
After the migration has been published then run the migrations to create required tables:
php artisan migrate
Usage
let's assume we have User model & Group model.
Using Applicant trait
to allow model behaves as applicant add Te7aHoudini\LaravelApplicant\Traits\Applicant trait to your model
use Illuminate\Foundation\Auth\User as Authenticatable; use Te7aHoudini\LaravelApplicant\Traits\Applicant; class User extends Authenticatable { use Applicant; // ... }
to make User model applies for Group model and creates a new application record in applications table . we can do this by different ways:
Using model for creating applications
if you didn't specify any application type or status. by default the type will be applicant and status wil be created
//create a record with default type of "applicant" and default status of "created" $user->appliesFor($group); //create a record with type of "randomAppType" and default status of "created" $user->appliesFor($group, ['type' => 'randomAppType']); //create a record with type of "randomAppType" and status of "randomAppStatus" $user->appliesFor($group, ['type' => 'randomAppType', 'status' => 'randomAppStatus']);
Using array for creating applications
in some cases you won't have a model object , but want to manually specify the attributes when creating application.
$user->appliesFor([ 'receiver_id' => 1, 'receiver_type' => 'App\Models\Group', ]); $user->appliesFor([ 'receiver_id' => 1, 'receiver_type' => 'App\Models\Group', 'type' => 'randomAppType', 'status' => 'randomAppStatus', ]);
Using null for creating applications
if you don't want to specify a model for creating applications then no problem.
//create a record with default type of "applicant" and default status of "created" //and both receiver_id & receiver_type are null $user->appliesFor();
Using hasAppliedFor()
if we want to check if model has made an application for another model. then we can achieve that with different ways.
note: like appliesFor() the hasAppliedFor() accepts same parameters
$user->hasAppliedFor($group); $user->hasAppliedFor($group, ['type' => 'randomAppType', 'status' => 'randomAppStatus']); $user->hasAppliedFor([ 'receiver_id' => 1, 'receiver_type' => 'App\Models\Group', ]); //check if $user model has application record with default type and status or not $user->hasAppliedFor();
Using appliedApplicationsFor()
if you want to get current model applied applications you can do like this:
note: like appliesFor() the appliedApplicationsFor() accepts same parameters
$user->appliedApplicationsFor($group)->get(); $user->appliedApplicationsFor($group, ['type' => 'randomAppType', 'status' => 'randomAppStatus'])->get(); $user->appliedApplicationsFor([ 'receiver_id' => 1, 'receiver_type' => 'App\Models\Group', ])->get();
Using appliedApplications()
this is a morphMany relation between current model and Application model
//returns \Illuminate\Database\Eloquent\Relations\MorphMany $user->appliedApplications();
Using applicantCriteria Attribute
some models maybe applies for specific application type or status , so to make it easy for overriding default application type and status . just define applicantCriteria attribute in your model
use Illuminate\Foundation\Auth\User as Authenticatable; use Te7aHoudini\LaravelApplicant\Traits\Applicant; class User extends Authenticatable { use Applicant; //you can define type or status or both $this->applicantCriteria = [ 'type' => 'randomAppType', 'status' => 'randomAppStatus', ]; } //instead of this. $user->appliesFor($group, ['type' => 'randomAppType', 'status' => 'randomAppStatus']); $user->hasAppliedFor($group,['type' => 'randomAppType', 'status' => 'randomAppStatus']); $user->appliedApplicationsFor($group, ['type' => 'randomAppType', 'status' => 'randomAppStatus'])->get(); //you can now just remove the second param. //and by default this will set the type to "randomAppType" and status to "randomAppStatus" $user->appliesFor($group); $user->hasAppliedFor($group); $user->appliedApplicationsFor($group)->get();
Using setApplicantCriteria()
if you want to set the applicant criteria dynamically per model. you can do this.
//this will create a record with type of "randomAppType" and status of "randomAppStatus" $user->setApplicantCriteria([ 'type' => 'randomAppType', 'status' => 'randomAppStatus', ])->appliesFor($group);
Using ReceivesApplications trait
to allow model behaves as receiver add Te7aHoudini\LaravelApplicant\Traits\ReceivesApplications trait to your model
use Illuminate\Database\Eloquent\Model; use Te7aHoudini\LaravelApplicant\Traits\ReceivesApplications; class Group extends Model { use ReceivesApplications; // ... }
to make Group model process application from User model and update existing application record in applications table . we can do this by different ways:
Using model for processing applications
if you didn't specify any application type or status. by default will query of type will applicant and status of created
//update existing record with default status of "processed" . $group->processApplicationFrom($user); //instead of updating with default status of "processed" then it will get updated by "accepted" status. $group->processApplicationFrom($user, 'accepted'); //query by type of "randomAppType" and update status to "processed" $group->processApplicationFrom($user, ['type' => 'randomAppType']); //query by type of "randomAppType" and update status to "accepted" $group->processApplicationFrom($user, ['type' => 'randomAppType'], 'accepted'); //query by type of "randomAppType" and status of "randomAppStatus" $group->processApplicationFrom($user, ['type' => 'randomAppType', 'status' => 'randomAppStatus']); //query by type of "randomAppType" and status of "randomAppStatus" and update status to "accepted" $group->processApplicationFrom($user, ['type' => 'randomAppType', 'status' => 'randomAppStatus'], 'accepted');
Using array for processing applications
in some cases you won't have a model object , but want to manually specify the attributes when processing application.
$group->processApplicationFrom([ 'applicant_id' => 1, 'applicant_type' => 'App\Models\User', ]); $user->processApplicationFrom([ 'applicant_id' => 1, 'applicant_type' => 'App\Models\User', 'type' => 'randomAppType', 'status' => 'randomAppStatus', ]); //you can override the default status of "processed" by providing second param. $group->processApplicationFrom([ 'applicant_id' => 1, 'applicant_type' => 'App\Models\User', ], 'accepted');
Using hasReceivedApplicationFrom()
if we want to check if model has received an application from another model. then we can achieve that with different ways.
note: like processApplicationFrom() the hasReceivedApplicationFrom() accepts same parameters except the last parameter of newStatus
$group->hasReceivedApplicationFrom($user); $group->hasReceivedApplicationFrom($user, ['type' => 'randomAppType', 'status' => 'randomAppStatus']); $group->hasReceivedApplicationFrom([ 'applicant_id' => 1, 'applicant_type' => 'App\Models\User', ]);
Using receivedApplicationsFrom()
if you want to get current model received applications you can do like this:
note: like processApplicationFrom() the receivedApplicationsFrom() accepts same parameters except the last parameter of newStatus
$group->receivedApplicationsFrom($user)->get(); $group->receivedApplicationsFrom($user, ['type' => 'randomAppType', 'status' => 'randomAppStatus'])->get(); $group->receivedApplicationsFrom([ 'applicant_id' => 1, 'applicant_type' => 'App\Models\User', ])->get();
Using receivedApplications()
this is a morphMany relation between current model and Application model
//returns \Illuminate\Database\Eloquent\Relations\MorphMany $user->receivedApplications();
Using receiverCriteria Attribute
some models maybe applies for specific application type or status , so to make it easy for overriding default application type and status . just define receiverCriteria attribute in your model
use Illuminate\Database\Eloquent\Model; use Te7aHoudini\LaravelApplicant\Traits\ReceivesApplications; class Group extends Model { use ReceivesApplications; //you can define type or status or both $this->receiverCriteria = [ 'type' => 'randomAppType', 'status' => 'randomAppStatus', ]; } //instead of this. $group->processApplicationFrom($user, ['type' => 'randomAppType', 'status' => 'randomAppStatus']); $group->hasReceivedApplicationFrom($user,['type' => 'randomAppType', 'status' => 'randomAppStatus']); $group->receivedApplicationsFrom($user, ['type' => 'randomAppType', 'status' => 'randomAppStatus'])->get(); //you can now just remove the second param. //and by default this will query by type of "randomAppType" and status of "randomAppStatus" $group->processApplicationFrom($user); $group->hasReceivedApplicationFrom($user); $group->receivedApplicationsFrom($user)->get();
Using setReceiverCriteria()
if you want to set the receiverCriteria criteria dynamically per model. you can do this.
//this will query by type of "randomAppType" and status of "randomAppStatus" $user->setReceiverCriteria([ 'type' => 'randomAppType', 'status' => 'randomAppStatus', ])->processApplicationFrom($group);
Testing
composer test
Changelog
Please see CHANGELOG for more information what has changed recently.
Security
If you discover any security related issues, please email ahmedabdelftah95165@gmail.com instead of using the issue tracker.
Credits
License
The MIT License (MIT). Please see License File for more information.
te7a-houdini/laravel-applicant 适用场景与选型建议
te7a-houdini/laravel-applicant 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 10 次下载、GitHub Stars 达 31, 最近一次更新时间为 2019 年 08 月 09 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「laravel」 「job」 「te7a-houdini」 「laravel-applicant」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 te7a-houdini/laravel-applicant 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 te7a-houdini/laravel-applicant 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 te7a-houdini/laravel-applicant 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
trix editor for laravel inspired by ActionText for rails
Provides a possibility to auto-setup crontabs required for project based on configuration file.
SlimQ Enables You to push jobs to background workers
PHP client for Kue priority job queue API
A lightweight task queue on top of rybakit/phive-queue
Laravel Relevance Ensurer
统计信息
- 总下载量: 10
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 31
- 点击次数: 8
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2019-08-09