Using Laravel to Toggle Element Visibility Based on Role in VueJS

<NavLink v-if="value":href="route('accounts')" :active="route().current('accounts')"> Accounts

Here is a scenario I encountered: within my user database, there are designated roles as either User or Admin.

My current focus revolves around:

If the user role is an admin, the value will be changed to TRUE.

I am facing challenges in extracting data from the database using Vue coding techniques.

Various attempts have been made using axios and other methods. However, I believe utilizing a method that retrieves the data and allows for comparison within an if statement would be more efficient.

Answer №1

For those utilizing laravel/breeze with inertia, accessing the current authenticated user data is possible by using usePage() or $page

If you need to access it within the <script></script> tags, you can utilize usePage()

const isAdmin = computed(() => {
    return usePage().props.auth.user.role === 'Admin'
})

Alternatively, if you require it within the template tag

<div v-if="($page.props.auth.user.role === 'Admin')"></div>

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

What is the best way to utilize node exec in synchronous mode?

I have a scenario where I need to run exec synchronously. Is there an alternative method to achieve this without using the deprecated execSync library? Can bluebird promises be used for this purpose? for (var i = 0; i < testCasesLength; i++) { va ...

What is the best way to export assets into a React Native JS file?

After watching a video on YouTube, I noticed that at 3:20 the individual in the video quickly exported multiple assets into a file without providing any explanation. Can someone please view this specific moment in the video and clarify how this person mana ...

Combining CSS and JavaScript to handle two events with a single onClick

Here is the code I've created: <a href="#" id="home" class="showLink" onclick="showHide('example');return false;"> <li class="buttons">home</li> </a> <a href="#" id="user" class="showLink" onclick="showHide(&a ...

Adjust the input values based on the child elements within the main container

Here is a series of DIVs with unique IDs: <div id="2988"> <div class="site">YaHoot.com</div> <div class="logo">/images/yahootLogo.jpg</div> //[and so on] <input type="button" value="YaHoot" onclick="javascript: ...

The fade-in effect will initiate from the start once the mouse leaves the element (with a

Currently, I am in search of a solution for improving the navigation menu I am developing. By clicking on this JSFiddle link, you can view the code in action. The issue I am facing is that the fadeIn effect should only occur when hovering over the nav-ite ...

Unable to store form data in a JSON file

i need help with the json data file called groups.json { "groups" : [ { "pname" : "group1", "remarks" : "best" }, { "pname" : "group2", "remarks" : "not the best" } , { "pname" : "group3", ...

Sending information upwards within an onClick event in a React component

Just starting out with React/ES6 and diving into creating my first components. Currently, I have a PuzzleContainer component that houses a Puzzle component responsible for displaying images. The container component triggers an AJAX call to fetch data on wh ...

Audio Playlists and Dynamic Loading with WordPress Shortcode

I'm looking to incorporate the wp_audio_shortcode() function using AJAX, and then customize the style of the audio player. However, I'm facing an issue where the HTML code returned by the AJAX request does not allow me to customize the audio play ...

Showing commute time using Google Maps API v3

Is there a way to display the travel time immediately when opening Google Maps API v3? If you want to show the driving time as soon as the map is loaded, check out this example that demonstrates switching between travel modes. While it's easy to set ...

Handling click events for parent and child elements in Angular 5

In Angular5, Below is the code I am working with: <div (click)="abc()"> some content <button (click)="xyz()">Click</button> </div> When I click on the button, both methods are being called. I want only a sin ...

Trouble with the width of the message container in CSS styling

I need to display a message at the top-center of my webpage. I have created a simple example for this: http://jsbin.com/eniwax/3/edit The issue I am facing is with the width of the message container. I want the container width to adjust dynamically based ...

Node interprets string as an object

I encountered the following code: <a class="btn-primary btn" href="/employer/booth/<%= user._id.toString() %>" <% console.log(typeof user._id, user._id) %> target="_blank"> Preview your booth </a> Upon running this ...

Crafting intricate designs using AngularJS

Currently, I am developing a web application utilizing AngularJS + SpringMVC. Due to restrictions in using only pure HTML view pages, server-side frameworks like Tiles are not viable options for me. I suspect that the angularJS ng-view tag may not be robu ...

What are the steps to ensure a form does not trigger the action URL and instead only prints the data upon submission

Currently, I am working on creating a form that will submit without opening the action URL when a button is clicked. Additionally, after submission, I want to display a message and clear the form. Can anyone guide me on how to achieve this? <form id="c ...

Nuxt encountering a critical issue: "module 'dir-glob' not found"

Recently, while running my Nuxt app, I encountered an error after restarting the server using npm run dev. The error message stated that it couldn't find the module 'dir-glob'. Despite finding a similar issue on Stack Overflow related to Ang ...

Vue Axios is failing to retrieve the response

I'm encountering an issue with Vue Axios. When attempting to send data using the get or put methods, I am unable to read it on the server side. Strangely, everything works fine when using POST. I'm stumped as to what the problem might be. Intere ...

Looking to retrieve data using Cheerio? If you're finding that the data appears empty in the page source but is visible when inspecting the elements, here's how you can go

I am encountering difficulties while trying to scrape data from another website. In my case, I notice that the data appears empty when viewing the page source, but it is visible when inspecting the elements. If you're confused, please refer to the fol ...

Vue.js2 - Detection of Observer in Array

A question for beginners in vue.js. I am trying to display data using the CanvasJS Library that is received via websocket. Everything works fine with the data until I introduce vue components into the mix. Let me clarify: export default { data() { r ...

The Bootstrap 4.5 code is not functioning as anticipated

I recently started learning Bootstrap and have been referring to the official documentation. However, I am facing issues with my code even though my folder only contains an index.html file. Link to Visual Studio Code screenshot. <!doctype HTML> < ...

What could be causing the Material UI tabs to malfunction when dynamically populating the content using a .map function instead of manually inserting it?

I was able to successfully integrate Material UI's tabs by manually adding content, but when I attempted to use a .map function to populate the content from a JSON data source, it stopped working. Can someone help me figure out why? The only change I ...