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 PSCAD安装问题 ERROR: Visual Studio 2013, 2015, 2017 or 2019 is not found in the system.
  • ¥15 (标签-MATLAB|关键词-多址)
  • ¥15 关于#MATLAB#的问题,如何解决?(相关搜索:信噪比,系统容量)
  • ¥500 52810做蓝牙接受端
  • ¥15 基于PLC的三轴机械手程序
  • ¥15 多址通信方式的抗噪声性能和系统容量对比
  • ¥15 winform的chart曲线生成时有凸起
  • ¥15 msix packaging tool打包问题
  • ¥15 finalshell节点的搭建代码和那个端口代码教程
  • ¥15 Centos / PETSc / PETGEM