Search code examples
phplaravelvuejs3laravel-bladeinertiajs

I have a laravel blade page with the @foreach, how can I transfer blade code to .vue page?


I have code on blade laravel and it contains @foreach. How can I port this blade code to .vue Inertia? I tried to send my data to array vue, but this be bad idea, or I'm idiot:(

index.blade.php code:

<x-app-layout>
    <x-slot name="header">
        <h2 class="font-semibold text-xl text-gray-800 dark:text-gray-200 leading-tight">
            {{ __('Appl to staff') }}
        </h2>
    </x-slot>
    @if (Session::has('success'))
        <div class="bg-lime-500 light:bg-lime-500 overflow-hidden shadow-sm sm:rounded-lg text-black text-center font-semibold">
            {{ Session::get('success') }}
        </div>
    @endif

    <div class="py-12">
        <div class="max-w-7xl mx-auto sm:px-6 lg:px-8">
            <div class="bg-white dark:bg-gray-800 overflow-hidden shadow-sm sm:rounded-lg">
                <div class="p-6 text-gray-900 dark:text-gray-100">
                    @foreach ($staff as $appl_to_staff)
                        <p>surname: {{ $appl_to_staff->surname}} </p>
                        <p>name: {{ $appl_to_staff->name}} </p>
                        <p>patronymic: {{ $appl_to_staff->patronymic}} </p>
                        <p>dob: {{ $appl_to_staff->dob}}</p>
                        <p><a class="font-bold" href="{{ route('staff.show', $appl_to_staff->id) }}">{{ __('More..') }}<a></p>
                        <br>
                    @endforeach
                </div>
            </div>
        </div>
    </div>
</x-app-layout>

Index.vue code:

<script setup>
import Welcome from '@/Components/Welcome.vue';
import AppLayout from '@/Layouts/AppLayout.vue';
import { Link } from '@inertiajs/vue3';

</script>

<template>
    <AppLayout title="Staff">
        <template #header>
            <h2 class="font-semibold text-xl text-gray-800 leading-tight">
                Staff
            </h2>
        </template>

        <div class="py-12">
            <div class="max-w-7xl mx-auto sm:px-6 lg:px-8">
                <div class="bg-white overflow-hidden shadow-xl sm:rounded-lg">
                    <div class="p-6 text-gray-900 dark:text-gray-100">
                        @foreach ($staff as $appl_to_staff)
                            <p>surname: {{ $appl_to_staff.surname}} </p>
                            <p>name: {{ $appl_to_staff.name}} </p>
                            <p>patronymic: {{ $appl_to_staff.patronymic}} </p>
                            <p>dob: {{ $appl_to_staff.dob}}</p>
                            <p><Link class="font-bold text-blue-600" href="{{ route('staff.show', $appl_to_staff->id) }}">{{ __('Подробнее..') }}</Link></p>
                            <br>
                        @endforeach
                    </div>
                </div>
            </div>
        </div>

    </AppLayout>
</template>

Controller

class StaffController extends Controller
{
    /**
     * Display a listing of the resource.
     */
    public function index()
    {
        $staff = Staff::all();
        return Inertia::render('Staff/Index', ['staff'=> $staff]);
    }

Index.vue on edit:

<script setup>
import Welcome from '@/Components/Welcome.vue';
import AppLayout from '@/Layouts/AppLayout.vue';
import { Link } from '@inertiajs/vue3';

</script>

<template>
    <AppLayout title="Staff">
        <template #header>
            <h2 class="font-semibold text-xl text-gray-800 leading-tight">
                Staff
            </h2>
        </template>

        <div class="py-12">
            <div class="max-w-7xl mx-auto sm:px-6 lg:px-8">
                <div class="bg-white overflow-hidden shadow-xl sm:rounded-lg">
                    <div class="p-6 text-gray-900 dark:text-gray-100">
                        <tr v-for="appl_to_staff in staff">
                            <td>surname: {{ appl_to_staff.surname}} </td>
                            <td>name: {{ appl_to_staff.name}} </td>
                            <td>patronymic: {{ appl_to_staff.patronymic}} </td>
                            <td>dob: {{ appl_to_staff.dob}}</td>
                            <td><Link class="font-bold text-blue-600" href="{{ route('staff.show', $appl_to_staff->id) }}">{{ __('Подробнее..') }}</Link></td>
                            <br>
                        </tr>
                    </div>
                </div>
            </div>
        </div>

    </AppLayout>
</template>

If you want see more another code message me in comments and i will send or edit post. Thank u!


Solution

  • You need to define the incoming props from the controller in your Vue code. So it will look something like this (reference)

    <script setup>
    import Welcome from '@/Components/Welcome.vue';
    import AppLayout from '@/Layouts/AppLayout.vue';
    import { Link } from '@inertiajs/vue3';
    
    defineProps({ staff: Array })
    </script>
    

    Then the code above should work. Just make sure to also add the key for you loop so it looks like this (reference)

    <tr v-for="appl_to_staff in staff" :key="appl_to_staff.id"></tr>