te7a-houdini / laravel-applicant
一个简单的包,允许模型申请并接收来自其他模型的申请
Requires
- php: ^7.0
- laravel/framework: ~5.5.0|~5.6.0|~5.7.0|~5.8.0|~6.0
Requires (Dev)
- orchestra/testbench: ^3.5|~4.0
This package is auto-updated.
Last update: 2024-08-27 02:29:43 UTC
README
一个简单的包,允许模型申请并接收来自其他模型的申请。
例如,您可以进行如下操作
//User model applies for Group model $user->appliesFor($group); //Group model process application from User model $group->processApplicationFrom($user,'accepted');
安装
您可以通过 composer 安装此包
composer require te7a-houdini/laravel-applicant
然后发布配置和迁移
php artisan vendor:publish --provider="Te7aHoudini\LaravelApplicant\LaravelApplicantServiceProvider"
迁移发布后,运行迁移以创建所需的表
php artisan migrate
使用
假设我们有一个 User 模型
和 Group 模型
。
使用 Applicant 特性
要使模型表现出申请者的行为,请将 Te7aHoudini\LaravelApplicant\Traits\Applicant
特性添加到您的模型中
use Illuminate\Foundation\Auth\User as Authenticatable; use Te7aHoudini\LaravelApplicant\Traits\Applicant; class User extends Authenticatable { use Applicant; // ... }
要使 User
模型向 Group
模型申请并创建一个新的 application
记录在 applications
表中,我们可以用不同的方式做到这一点
使用模型创建申请
如果您未指定任何申请类型或状态,则默认的 type
将为 applicant
,而 status
将为 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']);
使用数组创建申请
在某些情况下,您可能没有模型对象,但希望在创建申请时手动指定属性。
$user->appliesFor([ 'receiver_id' => 1, 'receiver_type' => 'App\Models\Group', ]); $user->appliesFor([ 'receiver_id' => 1, 'receiver_type' => 'App\Models\Group', 'type' => 'randomAppType', 'status' => 'randomAppStatus', ]);
使用 null 创建申请
如果您不想指定用于创建申请的模型,则没有问题。
//create a record with default type of "applicant" and default status of "created" //and both receiver_id & receiver_type are null $user->appliesFor();
使用 hasAppliedFor()
如果我们想检查模型是否已向另一个模型提交了申请,则可以通过不同的方式实现这一点。
注意:与 appliesFor() 类似,hasAppliedFor() 接受相同的参数
$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();
使用 appliedApplicationsFor()
如果您想获取当前模型的已提交申请,可以这样做
注意:与 appliesFor() 类似,appliedApplicationsFor() 接受相同的参数
$user->appliedApplicationsFor($group)->get(); $user->appliedApplicationsFor($group, ['type' => 'randomAppType', 'status' => 'randomAppStatus'])->get(); $user->appliedApplicationsFor([ 'receiver_id' => 1, 'receiver_type' => 'App\Models\Group', ])->get();
使用 appliedApplications()
这是当前模型和 Application
模型之间的一个 多态一对多关系
//returns \Illuminate\Database\Eloquent\Relations\MorphMany $user->appliedApplications();
使用 applicantCriteria 属性
某些模型可能需要申请特定的 type
或 status
,因此为了使覆盖默认申请 type
和 status
更容易,只需在您的模型中定义 applicantCriteria
属性即可
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();
使用 setApplicantCriteria()
如果您想按模型动态设置申请标准,可以这样做。
//this will create a record with type of "randomAppType" and status of "randomAppStatus" $user->setApplicantCriteria([ 'type' => 'randomAppType', 'status' => 'randomAppStatus', ])->appliesFor($group);
使用 ReceivesApplications 特性
要使模型表现出接收者的行为,请将 Te7aHoudini\LaravelApplicant\Traits\ReceivesApplications
特性添加到您的模型中
use Illuminate\Database\Eloquent\Model; use Te7aHoudini\LaravelApplicant\Traits\ReceivesApplications; class Group extends Model { use ReceivesApplications; // ... }
要使 Group
模型处理来自 User
模型的申请并更新 applications
表中的现有 application
记录,我们可以用不同的方式做到这一点
使用模型处理申请
如果您未指定任何申请类型或状态,则默认的 type
查询将为 applicant
,而 status
将为 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');
使用数组处理申请
在某些情况下,您可能没有模型对象,但希望在处理申请时手动指定属性。
$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');
使用 hasReceivedApplicationFrom()
如果我们想检查模型是否已从另一个模型接收了申请,则可以通过不同的方式实现这一点。
注意:与 processApplicationFrom() 类似,hasReceivedApplicationFrom() 接受相同的参数,除了最后一个参数 newStatus
$group->hasReceivedApplicationFrom($user); $group->hasReceivedApplicationFrom($user, ['type' => 'randomAppType', 'status' => 'randomAppStatus']); $group->hasReceivedApplicationFrom([ 'applicant_id' => 1, 'applicant_type' => 'App\Models\User', ]);
使用 receivedApplicationsFrom()
如果您想获取当前模型接收到的申请,可以这样做
注意:与 processApplicationFrom() 类似,receivedApplicationsFrom() 接受相同的参数,除了最后一个参数 newStatus
$group->receivedApplicationsFrom($user)->get(); $group->receivedApplicationsFrom($user, ['type' => 'randomAppType', 'status' => 'randomAppStatus'])->get(); $group->receivedApplicationsFrom([ 'applicant_id' => 1, 'applicant_type' => 'App\Models\User', ])->get();
使用 receivedApplications()
这是当前模型和 Application
模型之间的一个 多态一对多关系
//returns \Illuminate\Database\Eloquent\Relations\MorphMany $user->receivedApplications();
使用 receiverCriteria 属性
某些模型可能适用于特定的应用类型
或状态
,因此为了便于覆盖默认的应用类型
和状态
,只需在你的模型中定义receiverCriteria
属性即可。
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();
使用 setReceiverCriteria()
如果你想为每个模型动态设置receiverCriteria标准,可以这样做。
//this will query by type of "randomAppType" and status of "randomAppStatus" $user->setReceiverCriteria([ 'type' => 'randomAppType', 'status' => 'randomAppStatus', ])->processApplicationFrom($group);
测试
composer test
更新日志
有关最近更改的更多信息,请参阅更新日志。
安全性
如果你发现任何安全相关的问题,请通过电子邮件ahmedabdelftah95165@gmail.com联系,而不是使用问题跟踪器。
致谢
许可证
MIT许可证(MIT)。有关更多信息,请参阅许可证文件。