laravel 在laravel中如何将额外数据传递给mutators和accessors

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/37690538/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 13:57:09  来源:igfitidea点击:

In laravel how to pass extra data to mutators and accessors

phprestlaravellaravel-5

提问by Mustafa Dwekat

What I'm trying to do is to append the comments of each article to the articles object, but the problem is that I need to request different number of comments each time.

我想要做的是将每篇文章的评论附加到文章对象,但问题是我每次都需要请求不同数量的评论。

and for some reason I need to use mutatorsfor that, because some times I request 50 articles and I don't want to loop through the result and append the comments.

出于某种原因,我需要为此使用修改,因为有时我请求 50 篇文章,但我不想遍历结果并附加评论。

So is it possible to do something like the following and how to pass the extra argument.

那么是否可以执行以下操作以及如何传递额外参数。

This the Model:

这是模型:

    class Article extends Model
    {

        protected $appends = ['user', 'comments', 'media'];

        public function getCommentsAttribute($data, $maxNumberOfComments = 0)
        {
            // I need to set maxNumberOfComments
            return $this->comments()->paginate($maxNumberOfComments);

        }
    }

Here is the controller:

这是控制器:

class PostsController extends Controller
{


    public function index()
    {
        //This will automatically append the comments to each article but I
        //have no control over the number of comments
        $posts = Post::user()->paginate(10);
        return $posts;
    }    

}

What I don't want to do is:

我不想做的是:

class PostsController extends Controller
{


    public function index()
    {

        $articles = Post::user()->all();

        $number = 5;
        User::find(1)->articles()->map(function(Article $article) {
            $article['comments'] = $article->getCommnets($number);
            return $article;
        });

        return Response::json($articles);
    }    

}

Is there a better way to do it? because I use this a lot and it does not seams right.

有没有更好的方法来做到这一点?因为我经常使用它并且它接缝不正确。

回答by Denis Mysenko

Judging from the Laravel source code, no – it's not possible to pass an extra argument to this magic accessor method.

从 Laravel 源代码来看,不——不可能向这个神奇的访问器方法传递额外的参数。

The easiest solution is just to add another, extra method in your class that does accept any parameters you wish – and you can use that method instead of magic property.

最简单的解决方案是在您的类中添加另一个额外的方法来接受您希望的任何参数——并且您可以使用该方法而不是魔法属性。

Eg. simply rename your getCommentsAttribute()to getComments()and fire ->getComments()instead of ->commentsin your view, and you are good to go.

例如。只需将您的getCommentsAttribute()togetComments()和 fire->getComments()而不是->comments在您的视图中重命名,您就可以开始了。

回答by Juan Angel

I know its an old question, but there is another option, but maybe not the best:

我知道这是一个老问题,但还有另一种选择,但可能不是最好的:

$articles = Post::user()->all();

$number = 5;

$articles->map(function($a) use($number){ 
    $a->commentsLimit = $number;
    return $a;
});

And then in getCommentsAttribute():

然后在 getCommentsAttribute() 中:

return $this->comments()->paginate($this->commentsLimit);