Php Laravel:事件侦听器,getinsertid
我在实现Laravel的事件侦听器以获取我保存到db中的最后一个标题的插入时遇到问题。每次用户在我的数据库中插入标题时,我想向用户发送一封电子邮件,标题中包含记录的{id}。我认为我没有正确地从create()函数返回{id}Php Laravel:事件侦听器,getinsertid,php,laravel-4,Php,Laravel 4,我在实现Laravel的事件侦听器以获取我保存到db中的最后一个标题的插入时遇到问题。每次用户在我的数据库中插入标题时,我想向用户发送一封电子邮件,标题中包含记录的{id}。我认为我没有正确地从create()函数返回{id} public function store() { $input = Input::except('_token'); $this->title->create($input); $input['email'] =
public function store()
{
$input = Input::except('_token');
$this->title->create($input);
$input['email'] = $email;
$this->mailer->sendMail($email, $id);
return Redirect::back()->withSuccess( trans('main.created successfully') );
}
public function create(array $input)
{
foreach ($input as $k => $v)
{
$this->title->$k = $v;
}
$this->title->save();
return $id = $title->id;
Event::fire('Titles.Created', array($this->title, Carbon::now()));
}
这些问题很明显,例如:
- 在create方法返回之前不会激发事件
- store()方法尝试传递未设置的$id
- create()尝试将$id设置为$title->id,但尚未设置$title
但是,如果没有关于这些方法在应用程序中的位置以及它们与$this->title的关系的信息,就很难确定如何纠正这些问题。您能告诉我们这些方法的位置吗:控制器中的store()和模型中的create()?还有什么是$this->title()、模型、存储库?$this->title是模型