我很难在数组中提取项目,例如尝试在帖子中检索帖子。
这是错误
请记住一切正常,我可以提取帖子,但我试图成功向用户显示删除按钮。
即时关注laravel政策,为发布帖子的用户显示删除按钮
laravel.com/docs/5.5/authorization#via-blade-templates
未定义的变量:post(查看: /Applications/MAMP/htdocs/eli42/resources/views/home.blade.php)
这是我到目前为止所拥有的
public function index()
{
$posts = Post::with('user')->get();
return view('home', compact('posts'));
}
home.blade.php
<div id="mypost" class="col-md-8 panel-default" ng-repeat="post in myposts ">
<div id="eli-style-heading" class="panel-heading"><% post.user.name %></div>
<div class="panel-body panel">
<figure>
<p> <% post.body %></p>
<p> <% post.created_at %></p>
</figure>
<span>
@foreach($posts as $post)
@if(Auth::user()->can('delete',$post) )
<i style="color:red;" class="glyphicon glyphicon-remove" ng-click="deletePost(post)">
</i>
@endif
@endforeach
<span><a href="/" data-toggle="modal" data-target="#editModal">Edit</a></span>
</span>
</div>
</div>
我不确定我做错了什么
它需要显示一个关闭按钮而不是3
答案 0 :(得分:3)
return将程序控制返回给调用模块。执行 在被调用模块的调用之后的表达式中继续。
您正在发送方法 getPosts 的请求,以收集帖子。因此,您不需要通过索引方法返回帖子 变量。然后你需要 getPosts 。
您正在使用 angular ,在这种情况下,您需要有关于删除可用性的变量,但需要在javascript中使用此变量。只需将其添加到控制器并发送到角度,然后使用角度为{em>显示/隐藏删除按钮的ng-if
属性。
在这种情况下,您需要更改getPosts
方法,因为它会在每个帖子中添加可删除变量,然后您可以使用{{1在角度。您需要结束此任务的是将post.deletable
添加到ng-if="post.deletable"
标记,如果删除帖子可用,则显示此标记(按钮)。
控制器
<i>
看起来您正在使用Angular。对于这种情况,您的blade似乎很好。
在这种情况下,您视图中不需要public function getPosts()
{
return response()->json(Post::with('user')->get()->map(function(Post $post){
return collect($post->toArray())->put('deletable', auth()->user()->can('delete', $post));
}));
}
public function index()
{
return view('home');
}
$posts
,而中也不需要variable
叶片强>
@foreach
答案 1 :(得分:-3)
你应该试试这个:
<强>功能强>
public function index() {
$posts = Post::with('user')->get();
return view('home', compact('posts'));
}
<强> home.blade.php 强>
<div id="mypost" class="col-md-8 panel-default" ng-repeat="post in myposts ">
<div id="eli-style-heading" class="panel-heading"><% post.user.name %></div>
<div class="panel-body panel">
<figure>
<p> <% post.body %></p>
<p> <% post.created_at %></p>
</figure>
<span>
@foreach(posts as $post)
@if(Auth::user()->can('delete',$post->id) )
<i style="color:red;" class="glyphicon glyphicon-remove" ng-click="deletePost(post)">
</i>
@endif
@endforeach
<span><a href="/" data-toggle="modal" data-target="#editModal">Edit</a></span>
</span>
</div>
</div>