如何使用interiajs从控制器返回数据

发布于 2025-02-07 00:52:01 字数 464 浏览 2 评论 0原文

这是我的productController

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

方法

我正在寻找一种从惯性请求中返回此方法的

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

这是我的vue组件(不是我试图获取数据的方式的确切方式)

<template>

</template>

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

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>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

落日海湾 2025-02-14 00:52:01

在您的控制器中,您可以将产品列表传递给组件:

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

doc: https://inertiajs.com/responses

然后声明您的路线如:

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

这将创建/products路由,该路由将指向您的控制器的索引操作,该操作将将产品列表返回到您的组件。

在您的组件中,您可以访问产品:

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

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

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>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文