@nafeeur10 Tapping is used to do something with a variable before returning it.
So, imagine you’re fetching a model, want to call a method on that model, and then return the model. Traditionally, you might do something like this:
public function findArticleAndIncreaseView($id)
{
$article = Article::findOrFail($id);
$article->incrementViews();
return $article;
}
Instead of calling the method and then returning, we can use tap instead:
public function findArticleAndIncreaseView($id)
{
return tap(Article::findOrFail($id), function (Article $article) {
$article->incrementViews();
});
}
So, the first argument is the value. The second argument is a closure that will receive that value, inside which you can do what you want, before tap then returns the value.