【发布时间】:2023-03-09 22:52:01
【问题描述】:
我正在使用 Laravel 和 VueJS 开发一个简单的论坛。在显示属于该线程的所有帖子的线程视图上,我只想向登录用户发布的那些帖子显示编辑和删除按钮。因此,用户可以编辑和删除自己的帖子,但不能编辑和删除其他人。我已经搜索了 Laravel 和 VueJS 文档,以及 Laracasts 论坛和这里......但对我没有任何帮助(不确定我做错了什么)。
我同时使用 Laravel 的刀片模板和 Vue 组件。这是我的代码:
thread.blade.php:
@section('content')
<div class="header">
<h1>this is a thread</h1>
<forum-search ></forum-search>
</div>
<div class="container-block">
<thread :userId="{{ auth()->id() }}"></thread>
</div>
@endsection
thread.vue:
<template>
<!-- display this block of code ONLY when the user is logged in -->
<div style="margin-bottom: 10px" v-if="userId == post.user_id">
<!-- edit/delete buttons -->
</div>
</template>
<script>
export default {
name: 'thread',
components: { quillEditor },
props: ['userId'],
data(){}
</script>
我尝试了几种组合 <thread userId="{{ auth()->id }}"></thread>、<thread userId="{{ auth()->id() }}"></thread>、<thread userId="{{ Auth::user()->id }}"></thread> 或 <thread userId="{{ Auth::id() }}"></thread>,正如下面帖子中的人所建议的那样......但仍然没有按我的预期工作。有什么想法吗?
Laracast:https://laracasts.com/discuss/channels/vue/i-want-to-show-delete-button-only-to-its-post-owner-in-vuejs StackOverflow:How to get current authenticated user id using Vuejs and Laravel?
【问题讨论】:
-
使用
<thread :userId="{{ auth()->id() }}"></thread>你得到了什么? -
post.user_id设置在哪里? -
post.user_id来自我通过axios.get('/api/threads/' + thread_id)从我的api资源获得的json -
@N69S - 它说“未定义用户 ID”