Skip to content

Conversation

@kevinb1989
Copy link
Contributor

@kevinb1989 kevinb1989 commented Dec 13, 2025

When sending API requests to external services, we always have to assign the response to a temporary variable before working on that. making the Illuminate\Http\Client\Response class tappable will remove that hassle.

For example, I have a side project about movies. It is a good idea to only save IMDB id in the DB first, and then use it to retrieve the remaining data of a movie later.

Using tap in combination with macro, the code will be a lot cleaner.

use Illuminate\Http\Client\Response;

Response::macro(
    'movieFields',
    fn () => $this->collect()->only('title', 'year', 'runtime', 'director', 'plot')
);

class MoviesController extends Controller
{
    public function show(Movie $movie)
    {
        if ($movie->missingInformation()) {
            Http::post("http://www.omdbapi.com/?apikey=test_api_key&i={$movie->imdb_id}")
                ->throw()
                ->tap(fn (Response $response) => $movie->update($response->movieFields()));
        }

        return view('movies.show', compact('movie'));
    }
}

I also add a test case for adding macros to that Client Response class.

@taylorotwell taylorotwell merged commit 464de19 into laravel:12.x Dec 14, 2025
70 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants