The issue you're facing seems to be related to the way you're handling the event dispatch and the conditional rendering of the secondary form. Let's go through the potential problems and solutions:
-
Event Dispatching: Ensure that the event is correctly dispatched and listened to. In your
CreateCompanycomponent, you are dispatching an event namedcreatedCompany. However, in your Blade template, you are checking for$companyCreated, which is a boolean. You need to update this boolean when the event is dispatched. -
Updating State: After dispatching the event, you should update the
$companyCreatedstate totrueand assign the created company to$company. This will ensure that the conditional rendering in your Blade template works as expected. -
Blade Template: Ensure that the Blade template is correctly checking the
$companyCreatedstate. You should also ensure that the Livewire component is correctly passing the state to the Blade view.
Here's how you can adjust your code:
Update CreateCompany.php
Make sure to update the state after the company is created:
public function saveCompanyName()
{
if (!auth()->check()) {
$this->addError('authentication', 'You must be logged in to create a company.');
return;
}
try {
// Validate and create the company
$validatedData = $this->form->validate();
$company = $this->form->store(); // Store the company
if ($company && $company->id) {
// Assign user as admin and set as current company
auth()->user()->companies()->attach($company->id, ['role' => 'admin']);
auth()->user()->update(['current_company_id' => $company->id]);
// Update the state
$this->company = $company;
$this->companyCreated = true;
// Dispatch the event with the created company
$this->dispatch('createdCompany', $company);
} else {
Log::error('Company creation failed.');
$this->addError('company', 'Failed to create the company.');
}
} catch (ValidationException $e) {
foreach ($e->errors() as $field => $messages) {
foreach ($messages as $message) {
if (!$this->getErrorBag()->has($field)) {
$this->addError($field, $message);
}
}
}
} catch (\Exception $e) {
Log::error('An error occurred: ' . $e->getMessage());
$this->addError('exception', 'An error occurred while saving the company.');
}
}
Ensure Blade Template is Correct
In your create.blade.php, ensure that the conditional rendering is based on the $companyCreated state:
@if($companyCreated)
<!-- Business Info Form -->
@livewire('create-business-info', ['business_infoable' => $company])
@endif
Verify Event Handling
Ensure that the event createdCompany is correctly handled if you have any JavaScript or Livewire listeners that need to react to it.
By ensuring that the state is updated correctly and the Blade template checks the correct state, your form should reveal as expected after the company is created.