douyicao2199 2015-10-01 05:16
浏览 119
已采纳

Response.php第403行中的UnexpectedValueException - 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.
  • 写回答

2条回答 默认 最新

  • dongwen6743 2015-10-01 06:33
    关注

    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!

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?