1

This is my ProductController

public function index()
{
  return Product::all();
}

I'm looking a way to return this method from an Inertia request to my Vue component, This is the way I tried this,

routes/web.php

Route::get('/', function () {
  return Inertia::render('App', [ProductController::class, 'index']);
});

And here is my Vue component (Not the exact one the way I'm trying to get the data)

<template>

</template>

<script>
props: {
  index: Array,
},
</script>

1 Answer 1

2

In your controller, you can pass the list of your products to your component doing:

public function index()
{
  return Inertia::render('App', [
    'products' => Product::all()
  ]);
}

Doc: https://inertiajs.com/responses

Then declare your route like:

Route::get('/products', [ProductController::class, 'index']);

This will create a /products route that will point to the index action of your controller that will return the list of products to your component.

And in your component, you can access the products:

<template>
  <div>
    {{ products.length }}
  </div>
</template>

<script>
props: {
  products: Array,
},
</script>
Sign up to request clarification or add additional context in comments.

2 Comments

Your idea is great but this will only apply to the App page.
@BelugaB69 It's not my idea, it's the way it works. You can do this with any page. Just create a new page (component) and specify the name or path if you have created subfolders in the first argument of the render method. You create as many routes and controllers as you need.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.