Skip to content

Instantly share code, notes, and snippets.

@drakakisgeo
Created December 22, 2017 14:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save drakakisgeo/44f54ef8d65380fa228dd595d1281e1a to your computer and use it in GitHub Desktop.
Save drakakisgeo/44f54ef8d65380fa228dd595d1281e1a to your computer and use it in GitHub Desktop.
Working with Init Input and old() data from laravel in VueJs
//////---------- 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