【发布时间】: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