Laravel passed argument

Laravel passed argument

  1. Blog
  2. 15 November 2026
  3. 3 min read

Stefan Bogdanescu

Founder & Senior Architect

Laravel passed argument

The error you are encountering stems from a fundamental mismatch between how your controller expects to return data and what your scraper class is actually returning. The stack trace explicitly states that Symfony\Component\HttpFoundation\Response::setContent() received an object instead of a string or null. This happens because the index method in your ScraperController is returning an instance of the Amazon class itself, rather than the specific property inside it.

When you execute return $this->test; inside the controller, Laravel receives the entire App\Scraper\Amazon object. In a standard Blade view context or when returning JSON directly to the response stack, Laravel attempts to serialize this object. If the view engine tries to treat the return value as a string or if the underlying Response factory expects scalar data for content length calculations, it fails because the object does not implement the necessary interfaces or properties to be treated as raw string content in that specific context.

To resolve this, you need to ensure that the data flowing back to the view is in the correct format. In your Amazon class, the index method currently returns $this->test. While this variable holds the scraped text after processing, returning it directly from a controller action usually requires explicit handling to avoid ambiguity.

The most straightforward fix involves adjusting how the controller resolves the scraper instance and retrieves the data. Instead of storing the Amazon object in a public property and returning it, instantiate the scraper within the method scope where it is needed and return the specific string value. This ensures type safety and prevents Laravel from attempting to render an object as a view by default.

Here is how you should refactor the controller:

namespace App\Http\Controllers;

use App\Scraper\Amazon;
use Illuminate\Http\Request;
use Illuminate\View\View;
use Illuminate\Http\JsonResponse;

class ScraperController extends Controller
{
    public function index(): JsonResponse
    {
        $amazon = new Amazon();
        
        // Ensure the scraper runs and populates the result before returning
        $scrapedData = $amazon->index();

        return response()->json([
            'product_title' => $scrapedData,
            'status' => 'success'
        ]);
    }
}

In this implementation, we instantiate Amazon locally, call its logic to populate the $test property, and then explicitly wrap that string in a JSON response. This approach guarantees that the content type is handled correctly by Laravel's response stack.

Additionally, there is a structural issue in your original Amazon class regarding the constructor. The __construct method currently has a return type hint of string but returns a hardcoded literal "test" immediately after initialization. If this string is intended to be the final result, the scraping logic inside the index method will overwrite it anyway. However, mixing static construction with dynamic property assignment can lead to confusion about where data lives. It is cleaner to initialize properties in the constructor or rely on dependency injection if the scraper requires configuration like API keys or browser contexts.

Furthermore, using a public property $test to store intermediate scraping results is generally discouraged in PHP object-oriented design. It exposes internal state directly, making the class harder to test and maintain. A better pattern is to keep the data private and provide a dedicated getter method, such as public function getProductTitle(): string. This encapsulates the scraping logic and forces callers to interact with the class through its public interface.

By refactoring both the controller to return explicit JSON responses and updating the scraper class to use private properties with getters, you eliminate the type mismatch causing the TypeError and adhere to more robust coding standards within the Laravel ecosystem.

Our network