类 stdClass 的对象无法转换为字符串 Laravel 5

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/38716845/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 14:14:18  来源:igfitidea点击:

Object of class stdClass could not be converted to string Laravel 5

laravel

提问by Mohamed Amine

I have a problem when i make a query in blade laravel 5

我在 Blade 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

it gives me an error message like

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

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)

and what is the best way to do that

最好的方法是什么

采纳答案by ExohJosh

Your issue is coming from the $date variable - You are attempting to pass an object into the query and convert it to a string.

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

Your comment

你的评论

the value of $dates_chart_debit is array:3 [ 0 => {#235 +"date": "2016-07-14" } 1 => {#236 +"date": "2016-07-20" } 2 => {#237 +"date": "2016-07-29" } ]

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

Confirms this.

确认这一点。

If you replaced the:

如果您更换了:

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

with:

和:

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

Does it work?

它有效吗?

Also - Can you do dd($date); for me please?

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

回答by Mohamed Amine

I use Chart Js

我使用图表 Js

when i try to put default value of $date it show me nothing The character encoding of the HTML document was not declared. The document will be displayed with incorrect characters for some browser configurations if the document contains characters outside the US-ASCII range. Encoding page of characters must be stated in the document or in the transfer protocol.

当我尝试设置 $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
                    ]
            };