【问题标题】:Object of class stdClass could not be converted to string Laravel 5类 stdClass 的对象无法转换为字符串 Laravel 5
【发布时间】:2016-08-02 18:15:55
【问题描述】:

我在刀片 laravel 5 中进行查询时遇到问题

 @foreach($dates_chart_debit as $date)
<?php

$result =  DB::table('productions')
->join('durees', 'productions.duree_id', '=', 'durees.id')
->where(DB::raw("Date(productions.date)") ,"=",$date)
->where('productions.puit_id' ,"=",1)
->where(DB::raw("durees.isInReport") ,"=",'1')
->where('final_validation' ,"=",'1')
->select(DB::raw("sum(debit) as debit"))->first()->get();
?>

{{$result->debit}}
@endforeach

它给了我一个错误信息,比如

Object of class stdClass could not be converted to string (View: C:\Program Files\EasyPHP-DevServer-14.1VC9\data\localweb\projects\Coil\resources\views\panel\chart\executive.blade.php)

最好的方法是什么

【问题讨论】:

  • 什么是 var_dump($result);显示?
  • 查询中的问题我删除了 {{$result->debit}} 和同样的错误
  • 我在问 var_dump($result) 的输出是什么 - 这将有助于调试问题...
  • 同样的错误 类stdClass的对象无法转换为字符串(查看:C:\Program Files\EasyPHP-DevServer-14.1VC9\data\localweb\projects\Coil\resources\views\panel \chart\executive.blade.php)
  • $date 的值是多少?

标签: laravel


【解决方案1】:

您的问题来自 $date 变量 - 您正在尝试将对象传递到查询中并将其转换为字符串。

你的评论

$dates_chart_debit 的值为 array:3 [ 0 => {#235 +"date": "2016-07-14" } 1 => {#236 +"date": "2016-07-20" } 2 => {#237 +“日期”:“2016-07-29”}]

确认。

如果您更换了:

->where(DB::raw("Date(productions.date)") ,"=",$date)

与:

->where(DB::raw("Date(productions.date)") ,"=",2016-07-14)

有效吗?

另外 - 你能做 dd($date);请给我?

【讨论】:

  • 我不知道,因为我在 javascript 中有问题,它显示了一个空白页
【解决方案2】:

我用的是 Chart Js

当我尝试输入 $date 的默认值时,它什么也没有显示 HTML 文档的字符编码未声明。如果文档包含 US-ASCII 范围之外的字符,则对于某些浏览器配置,文档将显示不正确的字符。字符的编码页必须在文档或传输协议中说明。

var areaChartData = {
                labels: [
                    @if(isset($dates_chart_debit))
                           @foreach( $dates_chart_debit as $dd)
                                "{{$dd->date}}",
                            @endforeach
                     @endif
                    ],
                datasets: [
                        @if(isset($puits_chart_debit ))
                            @foreach($puits_chart_debit as $puit)
                            {
                                    label: "{{$puit->code}}",
                                    fillColor: "{{$puit->color}}",
                                    strokeColor: "{{$puit->color}}",
                                    pointColor: "{{$puit->color}}",
                                    pointStrokeColor:"{{$puit->color}}",
                                    pointHighlightFill: "#fff",
                                    pointHighlightStroke: "{{$puit->color}}",
                                    data: [

                                    @foreach($dates_chart_debit as $date)
                                       <?php

                                             $result =  DB::table('productions')
                                                    ->join('durees', 'productions.duree_id', '=', 'durees.id')
                                                    ->where(DB::raw("Date(productions.date)") ,"=",$date->date)
                                                    ->where('productions.puit_id' ,"=",1)
                                                    ->where(DB::raw("durees.isInReport") ,"=",'1')
                                                    ->where('final_validation' ,"=",'1')
                                                    ->select(DB::raw("sum(debit) as debit"))->first()->get();


                                            dd($result->debit);
                                       ?>


                                    @endforeach
                                           ]
                            },
                            @endforeach
                        @endif
                    ]
            };

【讨论】:

    猜你喜欢
    • 2020-03-16
    • 1970-01-01
    • 1970-01-01
    • 2021-05-28
    • 2017-09-21
    • 1970-01-01
    • 1970-01-01
    • 2011-04-06
    相关资源
    最近更新 更多