Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In laravel how to pass extra data to mutators and accessors

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 mutators for that, because some times I request 50 articles and I don't want to loop through the result and append the comments.

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.

like image 415
Mustafa Dwekat Avatar asked Jun 07 '16 22:06

Mustafa Dwekat


People also ask

How do you use accessor and mutator methods in Laravel?

Accessors and mutators allow you to format Eloquent attributes when retrieving them from a model or setting their value. For example, you may want to use the Laravel encrypter to encrypt a value while it is stored in the database, and then automatically decrypt the attribute when you access it on an Eloquent model.

What are accessors and Mutators explain with code snippets in Laravel?

Laravel Accessors and Mutators are custom, user defined methods. Accessors are used to format the attributes when you retrieve them from database. Whereas, Mutators are used to format the attributes before saving them into the database.

What is protected $casts in Laravel?

protected $casts = [ 'config' => 'array', ]; // ... } This way, whenever we use Eloquent to fetch projects from the database, each record will have that property as an array . And also, it will be converted back to json when trying to store/update records.

What is mutator in Laravel?

Defining A Mutator The mutator will receive the value that is being set on the attribute, allowing you to manipulate the value and set the manipulated value on the Eloquent model's internal $attributes property. So, for example, if we attempt to set the first_name attribute to Sally : $user = App\User::find(1);


Video Answer


1 Answers

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

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 ->comments in your view, and you are good to go.

like image 50
Denis Mysenko Avatar answered Oct 28 '22 23:10

Denis Mysenko