I have to make the update user profile view in Vuejs, the view is currently made in blade and it works fine, but I have to pass everything to Vuejs.
From the sidebar.blade.php I pass the user to the Vue component called Profile.vue
<profile-component :auth="{{ Auth::user() }}"></profile-component>
The data is collected in the template of the Profile.vue component and is displayed in the text boxes of the form with v-model .
<template>
<div class="row">
<form class="form-horizontal form-material">
<div class="form-group">
<label class="col-md-12">Nombre</label>
<div class="col-md-12">
<input type="text" class="form-control form-control-line"
v-model="auth.name">
</div>
</div>
<div class="form-group">
<label class="col-md-12">Apellido</label>
<div class="col-md-12">
<input type="text" class="form-control form-control-line"
v-model="auth.lastname">
</div>
</div>
<div class="form-group">
<label class="col-sm-12">Genero</label>
<div class="col-sm-12">
<select class="form-control form-control-line">
<option>Femenino</option>
<option>Masculino</option>
<option>Otra orientación</option>
</select>
</div>
</div>
<div class="form-group">
<label class="col-md-12">Sobre mi</label>
<div class="col-md-12">
<textarea rows="5" class="form-control form-control-line"
v-model="auth.description">
</textarea>
</div>
</div>
<div class="col-sm-12">
<button class="btn btn-success"
v-on:click="editUser()">Actualizar
</button>
</div>
</form>
</div>
</template>
<script>
export default {
props: ['auth'],
data() {
return {
user: {
firstName: '',
lastName: '',
gender: '',
descriptionUser: ''
},
}
},
methods: {
editUser(){
}
},
filters: {
moment: function(date) {
return moment(date).format("D [de] MMMM [de] YYYY ");
}
}
}
These are the paths of the we.php file
Route::get('/panel/profile', 'UserController@userEdit');
Route::put('/panel/profile', 'UserController@userUpdate');
How can I do it having the Vuejs view?
I need to create editUser() method
The topic confuses me since I've only been with Vue for a short time, I'm using Laravel 5.6, Vuejs 2 and Axios.
From already thank you very much
Simple, instead of using
:auth="{{ Auth::user() }}"
, make sure to remove the dots:
, since in this way you will be specifying to html that itauth
is just one more property of the element inside the DOM; It will also be necessary to transform the user to typeJSON
when passing it to the property:Problem inside Vue component
You will realize that even doing this your component will not work and that is because Vue will interpret the property value
auth
as a string, there are many ways to solve this, I will go for the simplest. In order to solve the property problem, it will be necessary to transform the data at some point in the component's life cycle and add it to a new data, an example could be:NOTE: You have to replace all use of
auth
insideuser
your template.Update information via AJAX request
Considering that you are using Axios, you would have to follow a process similar to the following:
Keep in mind for it to
axios
work you need to specify thecsrf token
within the configurations ofaxios
, if you use Laravel Mix, you will not have any problem since this already includes in the default configuration (within the bootstrap.js file ) the integration of said token.