在当前的Laravel应用程序中,我管理的项目都有一个文件文件夹。
在商店项目中,将调用创建的观察者来创建文件夹。到现在为止,效果很好。
在项目更新中,我想更新文件夹名称以使folder_name = project_name。
但是,如果我同时使用两个观察者,则在创建的观察者之前调用更新的观察者。为了显示它,我实现了echo
函数。
控制器:
public function store(StoreProjectRequest $request)
{
$project = $request->user()->projectsAsCreator()->create($request->all());
$projectPermission = new ProjectPermissionController();
$projectPermission->createPermissions('view', $project->id, [$request->user()->id]);
$projectPermission->createPermissions('edit', $project->id, [$request->user()->id]);
$projectPermission->createPermissions('admin', $project->id, [$request->user()->id]);
event(new ProjectCreated($project));
return response()->json(new ProjectCollectionResource($project));
}
模型方法
protected static function booted()
{
static::created(function ($project) {
self::setProjectNumber($project);
self::createProjectFolder($project);
self::updateProject($project);
echo '1';
});
static::updated(function ($project) {
echo '2';
self::updateFolder($project);
});
}
public static function updateProject($project)
{
$project->update([
'due_date_status' => self::getDueDateStatus($project),
'due_date_status_label' => self::getDueDateStatusLabel($project),
'issue_labels' => self::getIssueLabels($project->issues)
]);
}
响应
2{
"message": "Trying to get property 'objectable' of non-object",
"exception": "ErrorException",
"file": "/gopanel/sites/7industry_net/public/7app/app/Models/Project/ProjectMethod.php",
"line": 141,
"trace": [
{
"file": "/gopanel/sites/7industry_net/public/7app/app/Models/Project/ProjectMethod.php",
"line": 141,
"function": "handleError",
"class": "Illuminate\\Foundation\\Bootstrap\\HandleExceptions",
"type": "->"
},
我们看到,响应中只有数字2。
为什么首先没有数字?这意味着,为什么创建的观察者没有在更新的观察者之前触发?