Fetch & Pass data from controller to inertia view

Fetch & Pass data from controller to inertia view

A. Homecontroller.php

    public function test(){

        $data = DB::table('test')->get();

        return Inertia::render('Test', [
            'datas' => $data->map(function($d){
                return [
                'name' => $d->name,
                'id' => $d->id,
                'email' => $d->email,
                'edit_url' => route('data_edit', $d)
                ];

    }),

            'create_url' => route('data_create')
        ])->withViewData(['title' => 'Test Page']);

    }


B. Test.jsx
import React from "react";

function Test({datas, create_url}) {
    return (
        <div>
           {datas.map(data => <p key = {data.id}>{data.name}</p>)}
        </div>
    )
}

export default Test;
B. resources/view/app.blade.php
<title>{{$title ?? ' '}}</title>

Leave a Reply