mercredi 3 février 2016

Checking if model was modified using repositories

I update model using repository like this:

public function update($id, UpdateCompanyRequest $request)
{
    $attributes = $request->all();

    $oldCompany = $this->company->find($id);
    $newCompany = $this->company->update($id, $attributes);

    // Only if it was really updated
    event(new CompanyUpdated($oldCompany, $newCompany));

    return redirect()->route('companies.show', $id);
}

How can I compare if company was updated, including its relationships? For example, a company has an address and if I changed the address the event should be fired. But if nothing has been changed then I do not need to fire this event.

The only way I thought about manually checking each field/relationship which is ugly.

isDirty() works only before saving the model.



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire