ycs77 / laravel-wizard

A web Setup Wizard for Laravel application.
MIT License
122 stars 18 forks source link

Pass data between steps #7

Closed raphaelmsr closed 5 years ago

raphaelmsr commented 5 years ago

Hi, thanks for your reactivity for the udpate! I'm using your package and I need to have data from the 1st step shown in the 2nd step; how can I achieve that. Also my model is new User; , so I how do define my model in the next steps ? Since its not new and its to add more to the same - should I simply put new User; again ?

ycs77 commented 5 years ago

Hi! Thanks for using my package.

This package temporarily stores each Step data as an Array, not a storage Model. So you can use the following methods to solve:

Suppose there are now two Steps NameStep and EmailStep. First, don't set the Model for all Steps, but don't use the last one (is NameStep):

    public function model(Request $request)
    {
        //
    }

    public function saveData(Request $request, $data = null, $model = null)
    {
        //
    }

Next, receive all the data in the last Step and save Model (is EmailStep):

    public function model(Request $request)
    {
        return new User();
    }

    public function saveData(Request $request, $data = null, $model = null)
    {
        $data = $this->wizard->stepRepo()->original()->reduce(function ($carry, $step) {
            return array_merge($carry, $step->data());
        }, []);

        $model->fill($data)->save();
    }

Give it a try!

raphaelmsr commented 5 years ago

GREAT ! That works awesome. Maybe add this to the README, your package is fire. Now two more things, if you dont mind. First It doesnt save the data when I hit the back button; back functions as RESET, which would be a nice feature but I would rather just go back in history and keep the input data. Second, how can I show in the EmailStep blade file, information from NameStep. Can I reach to $this->wizard->stepRepo() within blade ?

EDIT : To pass data, I use session()->all() since I store in Session, not in DB. Same for the back button! Thanks!

ycs77 commented 5 years ago
  1. What do you mean by clicking Back, but executing Reaset, and not entering a record? If so, try adding a new Laravel 6 project to test. My test is successful back to the previous Step, and there is input data also.
  2. Just released v2.0.1, add the getRepo method to Step. You can use the $step->getRepo()->prev()->data('name'), $step->getRepo()->next()->data(), $step->getRepo()->first()->data('email') to get other step data on in the Blade. Detailed reference https://github.com/ycs77/laravel-wizard/blob/master/src/StepRepository.php.
  3. This package preset supports session and DB to store data. The default is session. In fact, $step->getRepo()->prev()->data('name') is the data retrieved from the session. Clear the data in the session when the Wizard executes to done.
raphaelmsr commented 5 years ago

Amazing, was doing this $session_info = isset(session()->all()["laravel_wizard"]) ? session()->all()["laravel_wizard"]["account"]["step_account"] : []; But I guess $step->getRepo()->prev()->data('name') takes care of all of this. Such great reactivity !

ycs77 commented 5 years ago

Happy use it! 😃