diff --git a/readme.md b/readme.md index a0fc782..a0fc23b 100644 --- a/readme.md +++ b/readme.md @@ -54,7 +54,7 @@ Wizard::routes('wizard/user', 'UserWizardController', 'wizard.user'); ### 2. Set steps -This is generated NameStep class, you can to `setModel` method get the model, to `rules` method set form validation, and save `$data` to your database via the `saveData` method: +This is generated NameStep class, you can to `model` method set the model, to `rules` method set form validation, and save `$data` to your database via the `saveData` method: *app/Steps/User/NameStep.php* ```php @@ -94,11 +94,11 @@ class NameStep extends Step * Set the step model instance or the relationships instance. * * @param \Illuminate\Http\Request $request - * @return void + * @return \Illuminate\Database\Eloquent\Model|\Illuminate\Database\Eloquent\Relations\Relation|null */ - public function setModel(Request $request) + public function model(Request $request) { - $this->model = User::find(1); + return User::find(1); } /** @@ -106,7 +106,7 @@ class NameStep extends Step * * @param \Illuminate\Http\Request $request * @param array|null $data - * @param \Illuminate\Database\Eloquent\Model|\Illuminate\Database\Eloquent\Relations\relation|null $model + * @param \Illuminate\Database\Eloquent\Model|\Illuminate\Database\Eloquent\Relations\Relation|null $model * @return void */ public function saveData(Request $request, $data = null, $model = null) @@ -192,9 +192,9 @@ protected $wizardOptions = [ If you want to customize the wizard base view, you can copy the view to `resources/views/user`. (`user` is `wizardName` property value on wizard controller), -### Set step relationships model +### Set relationships model of the step -Similarly, you can set the relationships model in `setModel` method of `Step`. +Similarly, you can set the relationships model in `model` method of the `Step`. ```php use Illuminate\Support\Arr; @@ -203,11 +203,11 @@ use Illuminate\Support\Arr; * Set the step model instance or the relationships instance. * * @param \Illuminate\Http\Request $request - * @return void + * @return \Illuminate\Database\Eloquent\Model|\Illuminate\Database\Eloquent\Relations\Relation|null */ -public function setModel(Request $request) +public function model(Request $request) { - $this->model = $request->user()->posts(); + return $request->user()->posts(); } /** @@ -215,10 +215,10 @@ public function setModel(Request $request) * * @param \Illuminate\Http\Request $request * @param array|null $data - * @param \Illuminate\Database\Eloquent\Model\Illuminate\Database\Eloquent\Relations\relation|null $model + * @param \Illuminate\Database\Eloquent\Model\Illuminate\Database\Eloquent\Relations\Relation|null $model * @return void */ -public function saveData(Request $request, $data = null,$model = null) +public function saveData(Request $request, $data = null, $model = null) { $data = Arr::only($data, ['title', 'content']); $model->create($data); diff --git a/src/Console/stubs/step.stub b/src/Console/stubs/step.stub index 272bc43..d951eb7 100644 --- a/src/Console/stubs/step.stub +++ b/src/Console/stubs/step.stub @@ -32,11 +32,11 @@ class DummyClass extends Step * Set the step model instance or the relationships instance. * * @param \Illuminate\Http\Request $request - * @return void + * @return \Illuminate\Database\Eloquent\Model|\Illuminate\Database\Eloquent\Relations\Relation|null */ - public function setModel(Request $request) + public function model(Request $request) { - // $this->model = + // } /** @@ -44,7 +44,7 @@ class DummyClass extends Step * * @param \Illuminate\Http\Request $request * @param array|null $data - * @param \Illuminate\Database\Eloquent\Model|\Illuminate\Database\Eloquent\Relations\relation|null $model + * @param \Illuminate\Database\Eloquent\Model|\Illuminate\Database\Eloquent\Relations\Relation|null $model * @return void */ public function saveData(Request $request, $data = null, $model = null) diff --git a/src/Http/Controllers/WizardController.php b/src/Http/Controllers/WizardController.php index 107ccc6..71da8ec 100644 --- a/src/Http/Controllers/WizardController.php +++ b/src/Http/Controllers/WizardController.php @@ -161,7 +161,7 @@ public function store(Request $request, string $step) if ($this->wizard()->option('cache')) { $step->cacheProgress($request); } else { - $step->saveData($request, $step->getRequestData($request), $step->model()); + $step->saveData($request, $step->getRequestData($request), $step->getModel()); } // Wizard step saved event. @@ -325,6 +325,7 @@ protected function getLastProcessedStepIndex(Request $request) */ protected function getWizardStep(Request $request, $slug) { + /** @var \Ycs77\LaravelWizard\Step|null $step */ try { if (isset($slug)) { $step = $this->wizard()->stepRepo()->find($slug); @@ -406,7 +407,7 @@ protected function save(Request $request) /** @var \Ycs77\LaravelWizard\Step $step */ foreach ($this->wizard()->stepRepo()->all() as $step) { $step->setModel($request); - $step->saveData($request, $step->data(), $step->model()); + $step->saveData($request, $step->data(), $step->getModel()); } $data = $this->wizard()->cache()->get(); diff --git a/src/Step.php b/src/Step.php index e785c0b..335aba4 100644 --- a/src/Step.php +++ b/src/Step.php @@ -16,7 +16,7 @@ abstract class Step /** * The step model instance or the relationships instance. * - * @var \Illuminate\Database\Eloquent\Model|\Illuminate\Database\Eloquent\Relations\relation|null + * @var \Illuminate\Database\Eloquent\Model|\Illuminate\Database\Eloquent\Relations\Relation|null */ protected $model; @@ -121,9 +121,9 @@ public function view() /** * Get the step model instance or the relationships instance. * - * @return \Illuminate\Database\Eloquent\Model|\Illuminate\Database\Eloquent\Relations\relation|null + * @return \Illuminate\Database\Eloquent\Model|\Illuminate\Database\Eloquent\Relations\Relation|null */ - public function model() + public function getModel() { return $this->model; } @@ -135,6 +135,17 @@ public function model() * @return void */ public function setModel(Request $request) + { + $this->model = $this->model($request); + } + + /** + * Set the step model instance or the relationships instance. + * + * @param \Illuminate\Http\Request $request + * @return \Illuminate\Database\Eloquent\Model|\Illuminate\Database\Eloquent\Relations\Relation|null + */ + public function model(Request $request) { // } @@ -144,7 +155,7 @@ public function setModel(Request $request) * * @param \Illuminate\Http\Request $request * @param array|null $data - * @param \Illuminate\Database\Eloquent\Model|\Illuminate\Database\Eloquent\Relations\relation|null $model + * @param \Illuminate\Database\Eloquent\Model|\Illuminate\Database\Eloquent\Relations\Relation|null $model * @return void */ abstract public function saveData(Request $request, $data = null, $model = null); diff --git a/tests/Stubs/PostStepStub.php b/tests/Stubs/PostStepStub.php index 5b5c08e..bb8ef91 100644 --- a/tests/Stubs/PostStepStub.php +++ b/tests/Stubs/PostStepStub.php @@ -33,11 +33,11 @@ class PostStepStub extends Step * Set the step model instance or the relationships instance. * * @param \Illuminate\Http\Request $request - * @return void + * @return \Illuminate\Database\Eloquent\Model|\Illuminate\Database\Eloquent\Relations\Relation|null */ - public function setModel(Request $request) + public function model(Request $request) { - $this->model = $request->user()->posts(); + return $request->user()->posts(); } /** @@ -45,7 +45,7 @@ public function setModel(Request $request) * * @param \Illuminate\Http\Request $request * @param array|null $data - * @param \Illuminate\Database\Eloquent\Model|\Illuminate\Database\Eloquent\Relations\relation|null $model + * @param \Illuminate\Database\Eloquent\Model|\Illuminate\Database\Eloquent\Relations\Relation|null $model * @return void */ public function saveData(Request $request, $data = null, $model = null) diff --git a/tests/Stubs/UserStepStub.php b/tests/Stubs/UserStepStub.php index 38baa5d..769563e 100644 --- a/tests/Stubs/UserStepStub.php +++ b/tests/Stubs/UserStepStub.php @@ -33,11 +33,11 @@ class UserStepStub extends Step * Set the step model instance or the relationships instance. * * @param \Illuminate\Http\Request $request - * @return void + * @return \Illuminate\Database\Eloquent\Model|\Illuminate\Database\Eloquent\Relations\Relation|null */ - public function setModel(Request $request) + public function model(Request $request) { - $this->model = $request->user(); + return $request->user(); } /** @@ -45,7 +45,7 @@ public function setModel(Request $request) * * @param \Illuminate\Http\Request $request * @param array|null $data - * @param \Illuminate\Database\Eloquent\Model|\Illuminate\Database\Eloquent\Relations\relation|null $model + * @param \Illuminate\Database\Eloquent\Model|\Illuminate\Database\Eloquent\Relations\Relation|null $model * @return void */ public function saveData(Request $request, $data = null, $model = null)