Tips for resolving the issue of table rows becoming detached in Laravel when using Vue.js: How can I prevent my table rows from being split

I am facing an issue while trying to create a table using Vue.js and Laravel. The problem is that each row of the table data is being displayed as a separate table.

Despite making changes to the code in both Vue and the controller, I cannot seem to resolve this issue and the result remains unchanged.

Below is the code from Customers.vue:

<template>
    <div>
        <h2>Customer Details</h2>
        <table class="table table-bordered" v-for="customer in customers" v-bind:key="customer.CustomerID">

        <tr>
            <th>ID</th>
            <th>Customer</th>
            <th>Address</th>
            <th>Contact No.</th>
        </tr>
            <tr>
                <td>{{customer.CustomerID}}</td>
                <td>{{customer.Customer}}</td>
                <td>{{customer.Address}}</td>
                <td>{{customer.CustomerContactNo}}</td>
            </tr>
        </table>
    </div>
</template>

<script>
export default {
    data() {
        return {
            customers: [],
            customer: {
                CustomerID: '',
                Customer: '',
                Address: '',
                CustomerContactNo: ''
            }, 
            customer_CustomerID:'',
            pagination: {},
            edit: false
        }
    },

    created (){
        this.fetchCustomers();
    },

    methods: {
        fetchCustomers() {
            fetch('api/customers')
                .then(res  => res.json())
                .then(res => {
                   this.customers = res.data;
                });
        }
    }
};
</script>

The code for the Controller (index) is provided below:

class CustomerController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        // Get customer details
        $customers = Customer::orderBy('created_at','desc')->paginate(5);

        //Return collection of Customers as a resource
        return CustomerResource::collection($customers);

    }

I anticipate the output of the table to be a single unified table instead of each row being rendered as a distinct table. Your help on resolving this issue would be greatly appreciated. Thank you.

Answer №1

The reason for the issue is that the v-for loop is positioned on the table element, resulting in a new table being created for each customer.

  • To resolve this, simply move the v-for directive to the tr element inside the customer section.

Check out the revised code segment below:

<template>
    <div>
        <h2>Customer Details</h2>
        <table class="table table-bordered">
        <tr>
            <th>ID</th>
            <th>Customer</th>
            <th>Address</th>
            <th>Contact No.</th>
        </tr>
            <tr v-for="customer in customers" v-bind:key="customer.CustomerID">
                <td>{{customer.CustomerID}}</td>
                <td>{{customer.Customer}}</td>
                <td>{{customer.Address}}</td>
                <td>{{customer.CustomerContactNo}}</td>
            </tr>
        </table>
    </div>
</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

Toastr Notification Failure in AJAX CRUD Operations

When attempting to display a toastr message in my AJAX CRUD, only the modal pops up and nothing is displayed within it. I have added all the necessary CDN links in the master layout and script tags as well. The data stored successfully in the database show ...

Here is a unique version: "Exploring the process of merging with groupBy in Laravel using PHP when dealing with two arrays, one of which contains all data

https://i.stack.imgur.com/OurWm.jpg https://i.stack.imgur.com/ehn40.jpg Is it possible to merge arrays with groupBy in Laravel PHP when one array lacks attributes and the other has additional properties? This scenario involves working with two different ...

Getting specific data from another table in Laravel using the pluck method is a useful feature. Would you like to

In my SalesController, I am aiming to retrieve a specific name from the Item table and then pass it on to an HTML file. This is what my controller method looks like: public function index() { $items = Item::orderBy('name', 'ASC')- ...

Enhance your Laravel project by isolating and extracting methods to improve performance

Currently, I am dedicated to a laravel project that is still ongoing and utilizes laravel 5.1. One particular process in the project takes more than 8 seconds to complete. This process includes a method with almost 250 lines of code. My question pertains ...

Using Laravel to effectively filter multiple values in a many-to-many relationship

I am currently developing a layered navigation module for my Laravel application, inspired by the functionalities found in Magento or WooCommerce. The main concept of this module is that products can have single or multiple attributes assigned to them, all ...

I'm looking for a way to dynamically update Laravel pagination records based on the selected option value for the number of items per page using Laravel and

I am working on creating a custom filter system using a select option menu and pagination with AJAX. The select option allows users to choose between displaying 10, 15, 20, or 25 products per page while updating the Laravel default pagination dynamically ...

Exploring Joins in Laravel 4 Query Builder

In my database, I have tables named "airport" and "route". The "id" of "airport" is a foreign key in "route" (i.e. Origin, Destination). Airport +-------+-------------+-----------------------+ | id | airportcode | Location | +-------+--- ...

Enhancing a One-To-Many Connection in Laravel's Eloquent System

Imagine I have a connection between these two Models in Laravel's Eloquent: <?php // user: // - user_id class User extends Model { protected $table = 'users'; public function settings() { ...

Establishing initial attribute values based on database schema

Can Laravel automatically set default attribute values for a model from the database when creating an object using new Model or Model::create()? For instance, I have a table named clients with several columns (such as name, sex, birthday, device, os, etc) ...

Steps for Displaying a Text with an Image in Laravel 4

Currently, I'm developing an application using Laravel 4. Within my controller, I have implemented a method called show(). This method retrieves the id from the route and creates an array containing specific elements fetched from the database. The arr ...

Implementing an npm-installed package in Laravel: A step-by-step guide

Apologies for the newbie question, but I'm completely new to package management systems, especially npm. Normally, I just link my CSS/Scripts to CDNs. However, I am currently working on a Laravel app that requires a more advanced <select> eleme ...

Using PHP to submit a file through an API

I'm currently working on creating an API endpoint to integrate with my Laravel and Vue application. public function avatar(Request $request) { $user = User::find(Auth::id()); $validator = Validator::make($request->all(), [ ...

Steps to include a personalized error message in Laravel when utilizing Rule::in for validation

I am currently working on validating a select dropdown in Laravel using an array of agency names. I have successfully implemented the validation using Rule::in(), which returns a standard error message 'The selected agency-name is invalid'. Howev ...

Running JavaScript in Laravel without explicitly calling it

I am facing a strange issue with my code. I have an input type="button" element along with a JavaScript function in the same page. The goal is to update a table column only when the user presses the button. However, the JavaScript function gets executed ev ...

Is the m:m Eloquent Relationship in Laravel 5.5 with a 1:1 variant valid in this scenario?

My goal is to establish a 1:1 relationship using the orgperson() function, even though there is an existing m:m relationship called orgs(). This will allow me to eagerly load a default organization. The objects involved are as follows, each with its own t ...

Laravel is powerfully integrated with PHP's infinite script capabilities

Looking for a way to continuously upload a table of users and relaunch the command directly after updating? Currently, I'm using Laravel with a cron job that runs every minute ($schedule->command('update:users')->everyMinute();), but t ...

Cross-origin request error persists despite configuring headers on the server. Unable to successfully relocate image to designated directory on the server

I am encountering a CORS error specifically when sending delete requests from Angular to Laravel. Additionally, I am facing issues with moving car model images to the directory during posting, resulting in errors. I have implemented a CORS middleware and a ...

Ensuring the validity of a file input through AJAX in a form built with Laravel

I am facing an issue with validating an image field in a Laravel form using AJAX. The field always appears to be empty even after selecting a file. How can I use ajax validation to check if the file input is empty or not? Here is the form code: {{ Form:: ...

What is the best method for incorporating a query in Ajax code while also passing parameter data?

I am currently working on a project in Laravel that involves using a create form with AJAX. After the form is completed, I need to merge data from two tables together within the AJAX code so that I can make specific selections. Here is an example of the AJ ...

Please refrain from displaying the user table data in the View page source under any script tags

When I access the user data in the script tag of my app.blade.php file, the page resources show this line of code. 'window.user = {"id":2,"name":"test","email":"hidden_email@example.com","email_verified_at":null,"created_at":"2021-03-02T04:52:43.0000 ...