Utilize Vuejs and Axios in Laravel to dynamically populate the form fields with the current user's information

I am looking to populate the form fields with current user information using Vuejs, Laravel, and Axios. Within the UserController.vue file, I have this code to retrieve the authenticated user's information from the API :

public function profile()
    {
        //
        return auth('api')->user();
    }

In the routes within the API.php file, I have defined the following endpoint :

Route::get('profile', 'API\UserController@profile');

On the profil.vue page, I have the following setup :

<script>
 import Form from 'vform';
    export default {
        data(){
            return {
              form: new Form({
              id : '',
              name : '',
              email : '',
              password : '',
              type : '',
              bio : '',
              photo : ''
            })
            }
        },
        mounted() {
            console.log('Component mounted.')
        },

        created() {
            // Displaying the currently logged-in user's information in the form
            axios.get("./api/profile")
            .then(({ data }) => (this.form.fill(data)));
        }
    }
</script>

I would like to show the current user's information in the form fields, for example :

<div class="form-group row">
     <label for="name" class="col-sm-2 control-label">Name</label>
          <div class="col-sm-12">
               <input v-model="form.name" type="text" class="form-control" id="name" name="name" placeholder="Name">
          </div>
</div>

I am using Laravel v7.6.2 and Vue.js 2

Thank you.

Answer №1

Your provided code appears to be in working order without any issues. The fill method from vform is capable of populating the form with retrieved data as expected.

To enhance your form, simply include various inputs and utilize the appropriate v-model directive within your template.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Discovering the value of a key within a JSON object by utilizing a String with JQuery

Given a JSON object, the challenge is to extract values based on user input. The input format will be similar to "data.location.type" or "data.location.items[1].address.street". Is it achievable using JQuery? { "data": { "location": { ...

Tricks for refreshing cached web page assets in the year 2023

So far, here are some of the old Cache Buster techniques I've come across: Adding a query string in the link src: /mstylesheet.css?cache_buster=12345 Changing the filename each time: /mstylesheet-12345.css Using Apache with Cache-Control "must-revali ...

Implementing bbcode feature in TinyMCE

Take a look at the custom html code provided below: <button type="button" class="btn btn-default btn-sm" onclick="appendBBCode('youtube','ContentArea')">[youtube]</button> <br> <div class="form-group field-boats-co ...

How to use the transform function in JavaScript

Hey there, I'm looking to create a new function. I am just starting out with javascript. I want to wrap the code below in a function like: ConfirmDelete() { }; This function should perform the same action as this code snippet: $('.btn-danger ...

Issue with HighCharts: Bar columns not extending to the x-Axis when drilling up

I am encountering an issue with HighChart/HighStock that I need help with. To illustrate my problem, I have set up a JSFiddle. The problem arises when a user drills down on a bar column, causing the y-axis to shrink and consequently making the x-axis appea ...

Issue with Angular7: Service worker not working properly

In my Angular service worker, I have successfully cached sounds (mp3 files) for offline playback. Below is the ngsw-config-json file configuration: { "index": "/index.html", "assetGroups": [{ "name": "app", "installMode": "prefetch", "reso ...

React: Applying the active class to mapped elements

I have a component that iterates over an array to generate 10 navigation items. I want to implement an onClick method that will add an active class to the clicked item. Are there any best practices using Hooks or the newer React patterns for achieving this ...

NodeJS buffer is not capable of handling incomplete TCP stream data

While troubleshooting my TCP JSON stream on the live server, I discovered that if the data streamed to me in JSON format is excessive, it doesn't consistently parse correctly. It requires multiple streams for successful parsing. Here is the code I am ...

Is there a way to confirm that a method was invoked using the internal testing framework?

Currently, I am utilizing the The Intern test framework to test my web application. My current challenge is verifying whether a method has been called during a test. I have searched for resources that demonstrate how this can be achieved using tools like ...

Additional non-essential attributes (e.g. title) were provided to the component but could not be utilized

runtime-core.esm-bundler.js?d2dd:38 [Vue warn]: The component received extraneous non-props attributes (title), which could not be automatically inherited due to rendering fragment or text root nodes. at <ProductTable title="Product List" & ...

The jQuery.ajax converter function is not being triggered

I'm struggling with jQuery.ajax converters - my converter function isn't triggering. This is the simplified jQuery AJAX code I'm using: $.ajax({ url: "http://myurl/myservice", dataType: "JSONP", cache: false, success: ...

Running Socket.io on a Web Hosting Service

As a newcomer to setting up webhost servers, I am currently studying socket.io. Despite having it run smoothly on my local server, I am unable to get it working on the live server. I'm at a loss on how to set this up properly. The issue persists when ...

How to change the max-width property of the <v-container> element

I recently set up Sass variables to customize the max-width of Vuetify's <v-container> like this: @use 'vuetify/lib/styles/main.sass' with ( $body-font-family: $body-font-family, $container-max-widths: ( 'md': 1190px, ...

What is the best way to include and transmit multiple records in ASP.NET MVC with the help of jQuery?

Having trouble sending multiple records to the database using JavaScript in ASP.NET MVC? Look no further, as I have the best code that can send data to the controller. However, the file attachment is not being sent along with it. I've tried various m ...

"Angular encountering an error with converting a string to a date due to

I have a date stored as a string in the format '2022-07-15T09:29:24.958922Z[GMT]'. When I try to convert it to a date object, I am getting an error saying it is an invalid date. Can anyone help me with this issue? const myDate = response; / ...

Execute the function when the form is submitted and show the total of the two values

I am currently working on a straightforward custom module for Drupal 7. This module takes two numeric values and upon submission, displays the sum of these values on the same page. While I have the form set up, it is not yet complete. I am facing some dif ...

The code snippets in the Vue3 documentation are quite peculiar

As I peruse the Vue 3 documentation, I notice a recurring pattern in how example code is presented for components: Vue.createApp({}) However, my experience with Vue 3 has been different. Instead of the above syntax, I simply use: <script> export d ...

Is it possible to hide a fixed header on scroll in a mobile device?

I am facing an issue with my Wordpress site where I want the header to hide when the user scrolls. Despite trying various codes, I have been unable to make it work. The reason for wanting to hide the header is that on mobile devices, it causes scroll prob ...

Content not being displayed by ng-repeat

Currently, I am in the process of developing a web application using Node.js with Express serving as the backend and HoganJs as the templating engine, while AngularJS is being utilized on the frontend. However, I have encountered an issue where the ng-repe ...

What is the process for integrating the node-menu package into my project without utilizing the require statement?

Is there a way to incorporate node-menu into my TypeScript project without using require, like this: const menu = require('node-menu'); Whenever I attempt to import node-menu into my project, I encounter the following errors: https://i.sstatic. ...