Please format your code correctly by wrapping it within triple backticks
```
// your code
```
Can someone tell me what I'm doing wrong?
DB: https://imgur.com/a/TI3ZnKE
web.php
Route::get('/', function () {
return Inertia::render('Welcome', [
'visibilities' => Visibility::all()
->map(fn($visible) => [
'id' => $visible->id,
'nameVisible' => $visible->nameVisible,
'commentVisible' => $visible->commentVisible,
'visible' => $visible->visible,
]),
]);
});
Vue
<script setup>
let props = defineProps({
visibilities: Array,
});
</script>
<template>
<div v-if="visibilities.id === 1 && visibilities.visible === 1">
<span> test text </span>
</div>
</template>
There is a lot to fix here.
Lets look at the server side first - I don't know if you need all of the record if you intend to show only the visible ones. In any case, there is no need for the map operation if you simply get the attributes you need:
Route::get('/', function () {
return Inertia::render('Welcome', [
'visibilities' => Visibility::select(['id', 'nameVisible', 'commentVisible', 'visible'])->get()
]);
});
On the client-side, the visibilities prop is an Array of Objects, but you are trying to access an individual Object's properties such as id and visible directly. You need to iterate over visibilities to interrogate each individual Object and its properties:
<script setup>
let props = defineProps({
visibilities: Array,
});
</script>
<template>
<template v-for="visibility in props.visibilities">
<div v-if="visibility.id === 1 && visibility.visible === 1">
<span> test text </span>
</div>
</template>
</template>
Alternatively, you can organize the data in a computed property by filtering the Array for the elements you need.
Ultimately, it is better to (i) fetch only the data you need from the database and (ii) send the minimum data to Inertia
Please or to participate in this conversation.