2 years ago

#58590

test-img

Charles Smith

Passing values within a multi-step form using Laravel 8 and Livewire 2.7

I have a multi-step form where a user can add cars to their profile. The first step is where the car is created and the relationship is established between the user and usercar. The first step works as it should, but it doesn't establish and or retain a connection to the usercar in subsequent steps. I know I am missing an obvious step of hydration or session code, but this is new territory for me.

AddUserCar.php

...
class AddUserCar extends Component
{
    public $showAnotherModel = false;

    // Steps
    public $totalSteps = 8;
    public $step = 1;
    // User
    public $super;
    public $first_name;
    public $last_name;
    public $email;
    public $status = 1;
    public $role = 'Driver';
    // Usercar
    public $car_id;
    public $calc_date;
    // Form Function
    public $car_year;

    //public $cars;
    //public $modelId;

    public function mount(User $user)
    {
        if(session()->has('tenant_id')) {
            $this->super = false;
        } else {
            $this->super = true;
            $this->tenants = Tenant::all()->pluck('first_name', 'id')->toArray();
        }

        $this->carYears = Car::all()
            ->unique('start_year')
            ->sortByDesc('start_year');

        $this->carModels = Car::all()
            ->unique('start_year')
            ->sortByDesc('start_year');

        $this->getModels();
    }

    ...

    public function moveAhead()
    {

        if($this->step == 1) {

            Usercar::create([
                'car_id' => $this->car_id,
                'user_id' => Auth::user()->id,
                'tenant_id' => Auth::user()->tenant_id,
                'calc_date' => now()->format('m-d-Y'),
            ]);
            dd($this->calc_date); // this produces an error
            
            $this->resetErrorBag();
            $this->resetValidation();
        }

        if($this->step == 2) {
            // Step Code
            $this->resetErrorBag();
            $this->resetValidation();
        }

        if($this->step == 3) {
            // Step Code

            $this->resetErrorBag();
            $this->resetValidation();
        }
        if($this->step == 4) {
            // Step Code

            $this->resetErrorBag();
            $this->resetValidation();
        }
        if($this->step == 5) {
            // Step Code

            $this->resetErrorBag();
            $this->resetValidation();
        }
        if($this->step == 6) {
            // Step Code

            $this->resetErrorBag();
            $this->resetValidation();
        }
        if($this->step == 7) {
            // Step Code

            $this->resetErrorBag();
            $this->resetValidation();
        }

        if($this->step == 8) {
            // Step Code

            redirect()->route('dashboard');
        }

        //Increase Step
        $this->step += 1;
        $this->_validateStep();
    }

    public function moveBack()
    {
        $this->step -= 1;
        $this->_validateStep();
    }

    private function _validateStep()
    {
        if ($this->step < 1) {
            $this->step = 1;
        }

        if ($this->step > $this->totalSteps) {
            $this->step = $this->totalSteps;
        }
    }

    public function render()
    {
        return view('livewire.add-user-car',
        ['pageTitle' => 'Add Driver Car']);
    }
}

php

laravel

laravel-livewire

0 Answers

Your Answer

Accepted video resources