[英]How do I access repository logic from the view?
Basically, I'm writing a simple blogging application where users can vote up or down on posts (I've named this process scoring inside my application). 基本上,我正在编写一个简单的博客应用程序,用户可以在其中投票赞成或反对帖子(我在应用程序内部将该过程评分)。 My problem is I am not sure what the best approach is for accessing a method to determine if the user has voted or not on the post, as I don't want to pass a repository into my view nor do I want the model to have methods mimicking the repository... Here are those two ideas - are these the only approaches?
我的问题是我不确定访问该方法以确定用户是否对该帖子投票的最佳方法是什么,因为我既不希望将存储库传递到视图中,也不希望模型具有模仿存储库的方法...这是两个想法-这些是唯一的方法吗?
The first approach requires that I pass a PostRepository
to my views as well as the Post
model which is already passed... 第一种方法要求我将
PostRepository
传递给我的视图以及已经传递的Post
模型...
<!-- Repository-in-view approach -->
<p>You voted {{ $postRepository->hasUserScored($post->id, $user->id) ? 'up' : 'down' }}.</p>
-----------
// Inside `PostRepository`
public function hasUserScored($postId, $userId, $vote = true)
{
// DB logic to determine ...
}
Or should I maybe do something like this? 还是我应该做这样的事情?
<!-- Repository-in-view approach -->
<p>You voted {{ $post->hasUserScored($user->id) ? 'up' : 'down' }}.</p>
-----------
// Inside `Post`
public function hasUserScored($userId)
{
return (new PostRepository)->hasUserScored($this->id, $userId);
}
// Inside `PostRepository`
public function hasUserScored($postId, $userId, $vote = true)
{
// DB logic to determine ...
}
What is the best way to overcome this? 克服此问题的最佳方法是什么? Any help greatly appreciated, thanks!
任何帮助,不胜感激,谢谢!
Best practice is to avoid calling functions from within views. 最佳实践是避免从视图内部调用函数。
Either call the function from the controller, and pass that data to the view. 从控制器中调用该函数,然后将该数据传递给视图。 Or, alternatively, use a view composer.
或者,也可以使用视图编辑器。
Why do you do this in such a complicated way? 你为什么要这么复杂地做呢? Can't your
Post
model just have many-to-many relationship with users + pivot table? 您的
Post
模型不能与用户+数据透视表建立多对多关系吗? Then, checking a User
vote is as simple as: 然后,检查
User
投票很简单:
function users()
{
return $this->belongsToMany('User')->withPivot('vote');
}
function getUserScore($userId)
{
return $this->users->find($userId)->pivot->vote;
}
Also, if you want to check whether a user has voted or not, simply do: 另外,如果要检查用户是否已投票,只需执行以下操作:
function hasUserVoted($userId)
{
return $this->users->contains($userId);
}
you have to pass the Repository to the view.. although what you want to do is not the smartest approach but according with what you need.. the way is 您必须将存储库传递给视图。.尽管您想要做的不是最聪明的方法,但是要根据您的需要..方式是
on your controller __construct you add something like this 在控制器__construct上,您可以添加如下内容
function __construct(PostRepositoryInterface $post){
$this->post = $post;
}
if you dont use interface then just pass the repository 如果您不使用接口,则只需通过存储库
then you return to the view 然后您返回视图
view(your.view)->with('post', $this->post);
then you are done.. 那你就完成了..
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.