From 9629771d22ddf6fb2e91287ba15080b24a7eba4d Mon Sep 17 00:00:00 2001 From: Mohit Panjwani Date: Tue, 30 Nov 2021 19:20:51 +0530 Subject: [PATCH] fix migration error --- ...62411_update_customer_id_in_all_tables.php | 78 +++++++++++-------- ...070204_add_owner_id_to_companies_table.php | 2 +- 2 files changed, 45 insertions(+), 35 deletions(-) diff --git a/database/migrations/2021_06_30_062411_update_customer_id_in_all_tables.php b/database/migrations/2021_06_30_062411_update_customer_id_in_all_tables.php index 06016faf..059a4e02 100644 --- a/database/migrations/2021_06_30_062411_update_customer_id_in_all_tables.php +++ b/database/migrations/2021_06_30_062411_update_customer_id_in_all_tables.php @@ -41,44 +41,54 @@ class UpdateCustomerIdInAllTables extends Migration }); } - $user->addresses->map(function ($address) use ($newCustomer) { - if ($address) { - $address->customer_id = $newCustomer->id; - $address->user_id = null; - $address->save(); - } - }); + if ($user->addresses()->exists()) { + $user->addresses->map(function ($address) use ($newCustomer) { + if ($address) { + $address->customer_id = $newCustomer->id; + $address->user_id = null; + $address->save(); + } + }); + } - $user->expenses->map(function ($expense) use ($newCustomer) { - if ($expense) { - $expense->customer_id = $newCustomer->id; - $expense->user_id = null; - $expense->save(); - } - }); + if ($user->expenses()->exists()) { + $user->expenses->map(function ($expense) use ($newCustomer) { + if ($expense) { + $expense->customer_id = $newCustomer->id; + $expense->user_id = null; + $expense->save(); + } + }); + } - $user->estimates->map(function ($estimate) use ($newCustomer) { - if ($estimate) { - $estimate->customer_id = $newCustomer->id; - $estimate->user_id = null; - $estimate->save(); - } - }); + if ($user->estimates()->exists()) { + $user->estimates->map(function ($estimate) use ($newCustomer) { + if ($estimate) { + $estimate->customer_id = $newCustomer->id; + $estimate->user_id = null; + $estimate->save(); + } + }); + } - $user->invoices->map(function ($invoice) use ($newCustomer) { - if ($invoice) { - $invoice->customer_id = $newCustomer->id; - $invoice->user_id = null; - $invoice->save(); - } - }); + if ($user->invoices()->exists()) { + $user->invoices->map(function ($invoice) use ($newCustomer) { + if ($invoice) { + $invoice->customer_id = $newCustomer->id; + $invoice->user_id = null; + $invoice->save(); + } + }); + } - $user->payments->map(function ($payment) use ($newCustomer) { - if ($payment) { - $payment->customer_id = $newCustomer->id; - $payment->save(); - } - }); + if ($user->payments()->exists()) { + $user->payments->map(function ($payment) use ($newCustomer) { + if ($payment) { + $payment->customer_id = $newCustomer->id; + $payment->save(); + } + }); + } } } diff --git a/database/migrations/2021_07_06_070204_add_owner_id_to_companies_table.php b/database/migrations/2021_07_06_070204_add_owner_id_to_companies_table.php index a4d76427..2aba3747 100644 --- a/database/migrations/2021_07_06_070204_add_owner_id_to_companies_table.php +++ b/database/migrations/2021_07_06_070204_add_owner_id_to_companies_table.php @@ -26,7 +26,7 @@ class AddOwnerIdToCompaniesTable extends Migration $companies = Company::all(); - if ($companies) { + if ($companies && $user) { foreach ($companies as $company) { $company->owner_id = $user->id; $company->slug = Str::slug($company->name);