Skip to content

Instantly share code, notes, and snippets.

@matyhaty
Created August 2, 2014 19:34
Show Gist options
  • Save matyhaty/e44e95358cecf1ca292c to your computer and use it in GitHub Desktop.
Save matyhaty/e44e95358cecf1ca292c to your computer and use it in GitHub Desktop.
Controller:
/**
* Update the specified resource in storage.
*
* @param int $id
* @return Response
*/
public function update($id)
{
$inputs = Input::get();
$inputs['userId'] = Auth::id();
$inputs['id'] = $id;
$this->publishDepotForm->validate($inputs, $id);
$depot = $this->execute(PublishDepotCommand::class, $inputs);
Flash::message("Your depot has been updated!");
return Redirect::back();
}
Model:
public static function publish($code, $title, $address)
{
$depot = new static(compact('code', 'title', 'address'));
$depot->raise(new DepotWasPublished($code, $title, $address));
return $depot;
}
public static function get($id)
{
$depot = Depot::find($id);
$depot->raise(new DepotWasUpdated($id));
return $depot;
}
Commander Hanler:
public function handle($command)
{
// Updating or Creating?
if($command->id)
{
$depot = Depot::get($command->id);
$depot->code = $command->code;
$depot->title = $command->title;
$depot->address = $command->address;
}
else
{
$depot = Depot::publish($command->code, $command->title, $command->address);
}
$depot = $this->depotRepository->save($depot, $command->userId);
$this->dispatchEventsFor($depot);
return $depot;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment