Reputation: 313
In this case, we have 3 tables:
tasks (task_id, task_title,task_type)
managers (manager_id, manager_title, manager_type)
assigners (task_id, manager_id)
In TaskController.php
, I have a store method to create new task
public function store() {
$input = Input::all();
$this->task->create($input);
return Redirect::route('tasks.index');}
Here's what I want to achieve:
task.created
, $task->task_title
with any $manager->manager_title
, if any matches found ($task->task_title
LIKE $manager->manager_title
), then pass the matched $task->task_id
and $manager->manager_id
as an array to another event,In an event listener located in start/global, it will create new record(s) in assigners table with values received from step2.The event listener is written as below:
Event::listen('task.created',function($param1,$param2){
$new_assigner = new Assigner;
$assigner->task_id = $param1;
$assigner->manager_id = $param2;
$new_assigner->save();
});
My questions:
Upvotes: 0
Views: 7434
Reputation: 1261
Since you need newly created task, it should be after the task has been created. May be after this line. You can pass newly created task as it is or, pass the task_id and fetch it in the event listener. But you have grab the task first.
$task = $this->task->create($input);
Event::fire('task.created', array($task) );
Using for each loop.
Event::listen('task.created', function($task)
{
$managers = getManagersForThisTask($task);
foreach($managers as $manager ){
Event::fire('task.created_step2', $task, $manager );
}
});
Upvotes: 2