TheRealPapa
TheRealPapa

Reputation: 4539

UnexpectedValueException in Response.php line 403 - Laravel 5.1

I am new to Laravel and am building a small project as my schooling. I have struck the above error and I cannot seem to fix it. Any help is appreciated.

In my controller I make the following call when first loading the page. The getDetails method works perfectly on page load, but fails with above error when I call it with Ajax as the user interacts with the page after load.

On page load the method is called and passed to it resulting values from earlier other method calls ($CSpec[0]->id and $CSize[0]->size_mm, which I can see as values 1 and 0.5) as follows:

    $CD = CS::getDetails($CSpec[0]->id, $CSize[0]->size_mm);

Also in the controller I have the following function which triggered via a route used in an Ajax call:

public function itemDetails($ct, $cs)
{
    return CS::getDetails($ct, $s);
}

The getDetails method looks like this:

public static function getDetails($ct, $cs)
{
    return DB::table('recommend_view')
        ->where('recommend_view.id', '=', $ct)
        ->where('recommend_view.size_mm', '=', $cs)
        ->first();
}

I have this route:

Route::get('vd/cd/{cd}/{cs}',
        ['uses' => 'Application\VDController@itemDetails', 'as' => 'application.vdcdetails']);

When I use Ajax to call the same method it fails with the above error. The Ajax code produces the following url in the $.getJSON call:

http://my.app/vd/cd/1/0.5

If I dd() inside getDetails for each variable I can see the correct values passed to the method from the url / controller.

If I execute the query in MySQL Workbench, it works as expected:

select * 
from `recommender_view` 
where `recommender_view`.`ct_id` = 1 
and `recommender_view`.`cs_size_mm` = 0.50 
limit 1;

So the method works perfectly when called on initial page load with variables fed to it directly from other method calls, but when the variables are passed via the URL / Ajax it fails and I cannot seem to understand what the difference is.

UnexpectedValueException in Response.php line 403:
The Response content must be a string or object implementing __toString(), "object" given.

Upvotes: 0

Views: 2090

Answers (2)

jellis
jellis

Reputation: 160

I believe I know what your issue is, however, please feel free to correct me.

What you're using is the Laravel query builder, which returns a stdClass object type in PHP, assuming it finds a result set for your query. This is effectively a "dumb" object that has no methods to "echo" or return. You can't cast to array, nor json etc.

So when the Laravel response object is trying to handle it, it can't use the __toString() method to deconstruct an appropriate response.

What you'd be better off doing is calling the query against a Model that extends the Illuminate Eloquent Model class.

In this instance, it COULD be something like this

CableRecommenderView::where('recommend_view.id', $ct)
    ->where('recommend_view.size_mm', $cs)
    ->first();

Eloquent is able to respond accordingly when being passed to the response object.

As an FYI, when using equals ('=') as the comparison operator in queries Eloquent (and possibly the query builder, although you'd have to check on that), you can forego explicitly defining it in the ->where() clause and simply pass the comparison variable as the second argument.

Hope that helps!

Upvotes: 1

Mwaa Joseph
Mwaa Joseph

Reputation: 763

The ajax call expects a response to be passed back to it. It is unable to recognize the object you are trying to pass to it.

Look into creating responses http://laravel.com/docs/5.1/responses.

If you return a json response e.g

public function itemDetails($ct, $cs)
{
    $item = CS::getDetails($ct, $s);
    return response()->json(['data' => $item]);
}

The ajax call should work. Then you can retrieve from the data variable from your ajax.

Upvotes: 0

Related Questions