lundi 29 février 2016

Laravel 5 - deleting child model data

I have a Model called Campaign which takes the following structure

+----+--------------+-----------------+----------+---------------+--------------+--------------------+----------+-------+--------+---------------------+---------------------+
| id | campaignName | userId          | clientId | clientContact | contactEmail | campaignObjectives | acNumber | notes | active | created_at          | updated_at          |
+----+--------------+-----------------+----------+---------------+--------------+--------------------+----------+-------+--------+---------------------+---------------------+
|  1 | test         |               7 |       10 | Mr Fakes      | 12345        | sdfsdfsd           | 12345    |       |      0 | 2016-02-29 11:51:59 | 2016-02-29 13:51:28 |
+----+--------------+-----------------+----------+---------------+--------------+--------------------+----------+-------+--------+---------------------+---------------------+

I then have a CampaignTypes Model with the following structure

+----+--------------+-----------------+------------+---------------------+---------------------+
| id | campaignType | creativeArrival | campaignId | created_at          | updated_at          |
+----+--------------+-----------------+------------+---------------------+---------------------+
| 14 | Dynamic      | 2016-02-26      |          1 | 2016-02-23 16:00:01 | 2016-02-23 16:00:01 |
+----+--------------+-----------------+------------+---------------------+---------------------+

The relationships in these Models is pretty straight forward. A Campaign can have many CampaignTypes and a CamapignType belongs to a Campaign.

In the Campaign schema I have an active column. This is what I use to delete a Campaign. So the destroy method looks like the following

public function destroy(Campaign $campaign)
{
    $campaign->update([
        'active' => false
    ]);

    Session::flash('flash_message', 'Campaign deleted');
    Session::flash('flash_type', 'alert-success');
    return Redirect::route('campaigns.index')->with('message', 'Campaign deleted.');
}

Now although it does not cause too many problems, I do not currently set any CampaignTypes row as being deleted if its parent Campaign has been deleted.

What would be the best way to delete the child data without actually deleting it?

Thanks



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire