【问题标题】:Laravel : Object of class stdClass could not be converted to stringLaravel:类stdClass的对象无法转换为字符串
【发布时间】:2020-03-16 07:08:06
【问题描述】:

我是使用 Laravel 的初学者,在这种情况下,我将从数据库中获取数据(价格),然后将其定义为变量($价格)以供进一步处理。

问题是当我定义变量 $price 并尝试回显它以测试结果时,它发生了一个名为“类 stdClass 的对象无法转换为字符串”的错误。

我这几天试图找出问题所在,意识到数据库的结果有问题。它更改为 stdClass 类的 Object 而不是数组?但是,当我使用我在互联网上找到的所有解决方案时,它仍然无法正常工作............

我已经尝试过的解决方案:

$array = json_decode(json_encode($price), true);

$price = array();

->toArray(); //Used in my code

下面是我的 Controller.blade.php

 /**
         * Update the specified resource in storage.
         *
         * @param  \Illuminate\Http\Request  $request
         * @param  \App\Menu  $menu
         * @return \Illuminate\Http\Response
         */
        public function update(Request $request, $day)
        {

            $opt1 = $request->post('dishopt1');

            $price1 = DB::table('dishes')->select('price', 'chg_porridge')->where([
                ['name', '=', $opt1],
            ])->get()->toArray();

            $price1 = $price1[0];

            echo $price1;

        }

【问题讨论】:

    标签: php arrays database laravel stdclass


    【解决方案1】:

    你可以通过使用value()方法避免做所有这些事情

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \App\Menu  $menu
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $day)
    {
    
        $opt1 = $request->post('dishopt1');
    
        $price1 = DB::table('dishes')->where([
            ['name', '=', $opt1],
        ])->value('price');
    
        echo $price1;
    
    }
    

    【讨论】:

    • 谢谢!你已经保存了我的项目!不知道它也有一个 ->value();函数很容易得到我想要的值!再次感谢!!
    【解决方案2】:
    $price1 = DB::table('dishes')->select('price', 'chg_porridge')->where([
                    ['name', '=', $opt1],
                ])->get();        
    $values = json_decode(json_encode($price1), true);
    

    【讨论】:

    • 请在您的代码中添加一些解释,以便其他人可以从中学习
    • 感谢您的回答,但我已经尝试了它仍然无法正常工作,顺便再次感谢您的帮助!
    【解决方案3】:

    DB::table()->get() 返回 StdClass 对象的集合(请参阅:What is stdClass in PHP?),这就是为什么您在选择第一个对象时会得到一个对象(请参阅:Laravel Query Builder - Retrieving Result)。

    这是 Laravel 有意为之的。它要么你简单地使用(array)$price1 进行响应,要么你使用Illuminate\Contracts\Routing\ResponseFactory 接口的->json() 方法,这会使你的返回如下:

    return response()->json($price1);
    

    注意:

    • 您不需要使用get() 然后转换为数组并检索第一项,您可以简单地使用->first() 而不是get(),然后您就有了第一个StdClass 对象。
    • 您也可以改用Eloquent model,它会在从数据库中检索时将其转换为与响应兼容的值。
    • 您可以只返回您的值,而不是使用echo。 Laravel 会为你创造奇迹。
    • 要测试一个值有什么你也可以使用dd() - dump and die 或dump()

    【讨论】:

    • 现在我终于知道是什么问题了 谢谢!!!但是,我仍然无法弄清楚为什么 Jason 编码和解码功能无法修复它。比如:$array = json_decode(json_encode($price), true);
    • 1. json_decode() 接受一个 json 字符串并将其转换为一个对象或数组(取决于你给它的标志)。 2.json_encode()将对象或数组转换为json字符串。这意味着您应该在解码之前进行编码,这就是您做错事的地方。但是,看起来调用这两个函数只是为了获取一个您想要返回的数组,因为响应是不必要的。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-05-28
    • 1970-01-01
    • 2017-09-21
    • 1970-01-01
    • 1970-01-01
    • 2011-04-06
    相关资源
    最近更新 更多