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 stm32流水灯+呼吸灯+外部中断按键
  • ¥15 将二维数组,按照假设的规定,如0/1/0 == "4",把对应列位置写成一个字符并打印输出该字符
  • ¥15 NX MCD仿真与博途通讯不了啥情况
  • ¥15 win11家庭中文版安装docker遇到Hyper-V启用失败解决办法整理
  • ¥15 gradio的web端页面格式不对的问题
  • ¥15 求大家看看Nonce如何配置
  • ¥15 Matlab怎么求解含参的二重积分?
  • ¥15 苹果手机突然连不上wifi了?
  • ¥15 cgictest.cgi文件无法访问
  • ¥20 删除和修改功能无法调用