Created
December 22, 2017 14:43
-
-
Save drakakisgeo/44f54ef8d65380fa228dd595d1281e1a to your computer and use it in GitHub Desktop.
Working with Init Input and old() data from laravel in VueJs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//////---------- in Controller @edit method | |
// set a variable with all needed initialized input (key value pairs) | |
$initInput = [] | |
$initInput = setInitInput($initInput); | |
// The setInitInput helper function, basically merges with old() input | |
function setInitInput(array $input){ | |
$oldInput = session()->getOldInput(); | |
unset($oldInput['_token']); | |
return json_encode(array_merge($input,$oldInput),JSON_UNESCAPED_UNICODE); | |
} | |
//////---------- In Laravel's blade view | |
<customcomponent :init-Input="{{ $initInput }}"></customcomponent> | |
// and the component | |
export default { | |
props: ['initInput'], | |
data: function () { | |
return { | |
... | |
}, | |
methods: { | |
initializeInput:function(){ | |
for (var key in this.initInput) { | |
if(this.hasOwnProperty(key) && this.initInput[key] !=""){ | |
this[key] = this.initInput[key]; | |
} | |
} | |
} | |
} | |
}, |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment