Having trouble integrating Vue component into a Laravel project?

My attempt to display messages from my database in a Laravel blade file using a Vue component is not working as expected. I'm referring to this Github repository https://github.com/kevilondo/group-chat for guidance. The data is being stored in the database and posting correctly, but the messages aren't showing up on the frontend.

I have limited experience with Vue.js.

The error message in the console says: Uncaught TypeError: Cannot read properties of undefined (reading 'prototype').

This is how my app.js file looks:

import Alpine from "alpinejs";
...

The contents of the bootstrap file:

window._ = require('lodash');
...

Description of the Message.vue file:

<template>
  <div v-if="user_id !== this.$userId" class="container card bg-info mb-4 float-start" style="width: 58%;">
...
</script>

Relevant part of show.blade.php:

 <div class="card-body container">
...

app.blade.php file:

 <body class="font-sans antialiased">
...
</body>

Controller method :

public function show_messages($id)
    {
        $group = Group::find($id);
...

Answer №1

I came across an error in the console that needs to be addressed. Here are some steps you can take to resolve it:

  • In the app.js file, make sure to import Vue from default.

window.Vue = require('vue').default;

  • Remove the import of bootstrap.js from app.blade.php since it is already being used in app.js.

    {{-- Remove next line --}}
    <script src="{{asset('js/bootstrap.js')}}"></script> 

    <script src="{{asset('js/app.js')}}"></script>

  • Update the show_messages method in MessageController.php as follows:

public function show_messages($id)
    {
        $group = Group::find($id);

        $messages = $group->messages()->with(['group', 'user'])->get();

        $user_loggedIn = auth()->user();

        return ['messages' => $messages, 'user_loggedIn' => $user_loggedIn];
    }

Answer №2

The issue seems to be stemming from this particular line in the GitHub repository, which is causing a problem due to its dependency on user login status.

@if (!Auth::guest())
        <meta name="user-id" content="{{ Auth::user()->id }}">
@endif

To address the situation where the user may not always be logged in, you can utilize the optional method as follows:

<meta name="user-id" content="{{ optional(Auth::user())->id }}">

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

When a global variable is defined after a return statement within a function declaration, it does

Check out this Javascript code snippet: http://jsfiddle.net/ramchiranjeevi/63uML/ var foo = 1; function bar() { foo = 10; return; function foo() {} } bar(); console.log(foo); // should return 10, but returns 1 It's interesting to no ...

The child chart component in echart remains static and does not get refreshed by vue

I just completed creating a Vue component that displays a PIE chart using the echart library. The initial setup includes setting default values for the PIE chart. The file for the PIE chart component is named pieChart.vue <template> <div :class ...

The property cannot be set because it is undefined in nodejs

var list = [ { title : '', author : '', content : '', } ] router.get('/japan',function(req,res){ var sql = 'select * from japan'; conn.query(sql,function(err,rows,fields){ for(var i = 0 ; ...

Issue with Boostrap collapse functionality not functioning correctly on live website, although it is working as intended in local environment

I am experiencing an issue with the collapsible navbar on my website. It is not closing smoothly as it should, despite being correctly implemented. The website is built using Bootstrap 4 and Jekyll, with a gulpfile for minifying and concatenating HTML, CSS ...

The $scope variable fails to reflect updates in the view following a broadcast event triggered by a

I have been troubleshooting a similar issue and I can't seem to figure out why the update is not reflecting in the view. While I am able to see the scope variable updating in the catch events logs, the changes are not being displayed in the view. For ...

What is the process of connecting JSON keys to one another?

I am currently brainstorming a solution to link two distinct JSON formats with their respective keys. Here are the two formats: { "price": [ 511, 499, 419, 312 ], "paid": "OK", "contract": "year", "begindate": "01/01/2018", " ...

Tips on saving information received from an API using Vue and axios into a variable that can be easily utilized in another tool or library such as chart.js

I'm struggling to save information from an API into a variable using Vue and axios. However, when I attempt to display the variable with document.write(this.info), it only shows undefined. How can I ensure that the variable contains data retrieved fro ...

What is the safest way for a function within a JavaScript hash to invoke another function in the same hash?

Below is a JavaScript/ES6 method that returns a hash with two methods - writeA and _write. The goal is to call writeA from outside of the hash, but this method needs to call _write, defined right below it, to complete its task. getHash = () => { re ...

Unable to clear JSP form data

While attempting to reset the form data in JSP, the code given below is run. Although the data gets reset successfully, it reappears mysteriously from an unknown source: $('input[type="text"]').val(''); ...

Nuxt - Component not navigating pages properly when using router.push

index.vue - <template> <div> <Header /> <div class="container"> <SearchForm /> </div> </div> </template> <script> const Cookie = process.client ? require('js-cookie&a ...

Python script utilizing Selenium to load only JavaScript content and excluding full HTML rendering

As I navigate through the link , I am on the lookout for the search bar marked with the class "search-field". The HTML snippet in question is as follows: from selenium import webdriver import time driver = webdriver.Firefox() driver.get("https://www.takea ...

Adjust the overall cost according to the quantity using jQuery or JavaScript

I'm currently working on a project that involves input fields for Product Price, Quantity Form Field, and displaying the Total price. My goal is to have the Total price automatically update based on the product price and quantity with jQuery. I am loo ...

Can a JavaScript function be handed to a constructor for an event handler?

The JavaScript function provided: function ShowFax() { $.ajax({ type: "POST", contentType: "application/json; charset=utf-8", url: "WebServices/SendFaxService.asmx/SaveFaxData", data: "{}", dataType: "json", ...

Making Life Easier with Netsuite: Streamlining Deposit Generation

Recently, I developed a Suitelet to streamline the process of applying deposits for Cash Sales. The idea was for users to upload a CSV file containing Cash sales records, which the script would automatically use to apply the deposits and create deposit r ...

Expanding the dropdown menu depending on the selection of the radio button

I am looking to implement a feature where the drop-down option is displayed or hidden based on the selection of a radio button in AngularJS. If the "Overall Company" radio button is selected, then the drop-down element should not be shown. If the "Demogra ...

Vue randomly sets the prototype of props in its definition

When the same code with the identical data structure is passed to the prop, it randomly sets the prop as an array or an object. The following is the code fragment: const props = defineProps({ all: { type: Array, default: [], } }) ...

How can I access the Blockly Factory code using the appropriate HTML syntax?

Utilizing VSCode and Block Factory tool at [https://blockly-demo.appspot.com/static/demos/blockfactory/index.html]. This is my code snippet in index.html: <html> <head> ... body { background-color: #fff; font-family ...

Trouble with v-form and v-text-field components in Vue CLI 3.0

Currently, I am working on a project using Vue Cli 3 and have decided to implement Vuetify for its UI. To integrate Vuetify into Vue Cli 3, all I had to do was run the command: vue add vuetify While I can easily use features like v-layout and v-button, I ...

It appears that the flex-grow property is not functioning as expected

I am working with a parent div and two children divs underneath it. I noticed that when I set the flex-grow property of the first child to zero, its width remains constant. However, if I add text to the second child, the width of the first child decreases. ...

Transfer information from the server to the client using NodeJS and Express

I am encountering an issue with sending data from my express server to the client side. I have implemented app.post and app.get, however the problem is that the request needs to originate from the client side. My requirement is to send the data from the se ...