Laravel Breeze惯性 - 在VUE页面中使用雄辩关系的问题

发布于 2025-02-07 10:16:13 字数 488 浏览 2 评论 0原文

首先,我已经使用了Laravel一段时间了,直到最近才选择尝试惯性vue.js构建,因为我想制作水疗中心...

但是,我似乎找不到Vue如何处理雄辩的关系。

我的意思是“香草” Laravel微风中的一个示例,我可以在用户表和帖子表之间建立关系。现在,在正常的Laravel中,我可以打开我的刀片页面,并访问使用代码创建帖子的用户:$ post- post- user-> name

在intertia vue版本中。由于我需要将所有帖子的数组作为vue页面的道具发送,因此它不会算作雄辩的对象,这意味着我不能使用$ post- post-> user-> name。 ..我已经查看了惯性文档(从我能找到的),无法找到他们执行此类请求的方式。

如果在惯性版本中根本不可能,我将简单地回到“香草” Laravel,因为对我来说,拥有雄辩的关系是必须的,我可以牺牲从水疗中心渲染的“即时”页面。

任何帮助将不胜感激!

First off, I have been using Laravel for a while now and only recently opted to try out the Inertia Vue.js build as I wanted to make a SPA...

However, I cannot seem to find how Vue handles Eloquent relationships.

An example of what I mean would be in "vanilla" laravel breeze, I could setup a relationship between my users table and the posts table. Now, in normal Laravel I could then open my Blade page and access the user who created a post by using the code: $post->user->name

In the Inertia Vue version. Since I need to send over the array of all posts as a prop to the Vue page, it wont count as an Eloquent object, which means I cannot use $post->user->name ... I have looked at the inertia documentation (from what I could find) and have not been able to find the way they managed to perform this type of request.

If this is simply not possible in the Inertia version, I will simply go back to the "vanilla" laravel, as having Eloquent Relationships is a must for me and I can sacrifice the "instant" page rendering from the SPA.

Any help would be appreciated!

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

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

发布评论

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

评论(1

掩于岁月 2025-02-14 10:16:14

您可以使用 api resource 来修改数据。以下示例:

yourcontroller.php

public function index() {
   return PostResource::collection(Post::with('user')->get());
}

postresource.php

public function toArray($request) {
    return [
        'id' => $this->id,
        'title' => $this->title,
        'user' => [
              'name' => $this->user->name,
              'email' => $this->user->email,
         ],
    ];
}

访问它,就像您在API Resource

index.vue.vue

// for post in posts
<li>{{ post.user.name }}</li>

上访问它一样,请始终限制返回资源的值,如果您有敏感的数据,例如CC编号或用户地址。因为从客户端可见返回值

You can modify the data with API Resource. Here's example:

YourController.php

public function index() {
   return PostResource::collection(Post::with('user')->get());
}

PostResource.php

public function toArray($request) {
    return [
        'id' => $this->id,
        'title' => $this->title,
        'user' => [
              'name' => $this->user->name,
              'email' => $this->user->email,
         ],
    ];
}

Access it like you format it on API resource

Index.vue

// for post in posts
<li>{{ post.user.name }}</li>

From my experience, always limit the return value of your resource if you have sensitive data like CC number or address of user. Because return value is visible from client side

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