当 运行 get() 使用 queryBuidler 时,class stdClass 的对象无法转换为字符串
when running get() using queryBuidler, Object of class stdClass could not be converted to string
我正在尝试 运行 这个 SQL 查询:
SELECT head_account_id, account_id, created_on, result, COUNT(*) AS counter FROM notes
WHERE result = 'not_processed' AND created_on >= (now() - INTERVAL 3 DAY) AND created_on <= (now() - INTERVAL 8 HOUR)
AND account_id IN (SELECT id FROM accounts WHERE account_status = 'approved' AND demo = 0)
GROUP BY account_id
ORDER BY head_account_id, account_id, created_on DESC;
使用 Laravel 查询构建器:
$appointments = DB::table('confirmed_appointments')
->select('head_account_id','account_id','created_on','result', DB::raw('count(*) as counter'))
->where('result', 'not_processed')
->where('created_on', '>=', DB::raw('now() - interval 3 day'))
->where('created_on', '<=', DB::raw('now() - interval 8 hour'))
->whereIn('account_id', $id)
->groupBy('account_id')
->orderBy('head_account_id')
->orderBy('account_id')
->orderBy('created_on', 'desc')
->get();
其中 $id 是:
$id = DB::table('accounts')
->select('id')
->where('account_status','Approved')
->where('demo','0')
->get();
上面的查询工作正常 - 我已经使用 dd() 进行了检查。
我 运行 在 $appointments= ..... -> get():
遇到错误
"Object of class stdClass could not be converted to string"
我知道 get() 通常 returns 一个 stdClass 对象,但我不明白为什么我根本不能使用 get()。许多其他解决方案说使用 get() -> toArray();但是我什至无法将 get() 正确地设置为 运行 ..如果我不对结果进行 运行 get() 和 运行 dd()有关查询的信息,但不是结果本身。
我是Laravel/PHP的新手,所以我可能缺乏知识,但我希望有人能帮助我。
感谢您的宝贵时间!
您可以尝试使用 pluck()
检索 id 值数组,即:
$id = DB::table('accounts')
->select('id')
->where('account_status','Approved')
->where('demo','0')
->pluck('id');
使用 get()
,您将在 $id 变量中检索标准对象的集合。
您 运行 进入该错误与 get 方法无关。它是关于你试图对该查询的结果做什么。如果您尝试回显 collection class 的结果,它会抛出该错误。你不能回显一个对象吧?
根据文档:
get() return是一个集合对象。这意味着它有一系列的结果。在您的情况下,它 return 集合对象包含任何对查询进行数学计算的记录 ID
first() return只有一个结果包含一条记录
如果你只想要一个 id 到 return 并且可以被视为 string ,这样你就可以在你的视图中回显它,你必须改用 first() 方法。
结论:
如果需要,可以使用 get(),并且必须对每个结果进行 foraech 循环。
如果你想要,可以只使用一个 id first();
但请注意,即使您首先使用它 returns std class,所以在这种情况下,如果您想访问 id。你必须写 $result->id
并回显它。
在您的情况下,$id
是匹配数据的整行,您只需要 id 列,因此您需要这样做
$id = DB::table('accounts')
->select('id')
->where('account_status','Approved')
->where('demo','0')
->get();
//The ID column
$id = $id->id;
我正在尝试 运行 这个 SQL 查询:
SELECT head_account_id, account_id, created_on, result, COUNT(*) AS counter FROM notes
WHERE result = 'not_processed' AND created_on >= (now() - INTERVAL 3 DAY) AND created_on <= (now() - INTERVAL 8 HOUR)
AND account_id IN (SELECT id FROM accounts WHERE account_status = 'approved' AND demo = 0)
GROUP BY account_id
ORDER BY head_account_id, account_id, created_on DESC;
使用 Laravel 查询构建器:
$appointments = DB::table('confirmed_appointments')
->select('head_account_id','account_id','created_on','result', DB::raw('count(*) as counter'))
->where('result', 'not_processed')
->where('created_on', '>=', DB::raw('now() - interval 3 day'))
->where('created_on', '<=', DB::raw('now() - interval 8 hour'))
->whereIn('account_id', $id)
->groupBy('account_id')
->orderBy('head_account_id')
->orderBy('account_id')
->orderBy('created_on', 'desc')
->get();
其中 $id 是:
$id = DB::table('accounts')
->select('id')
->where('account_status','Approved')
->where('demo','0')
->get();
上面的查询工作正常 - 我已经使用 dd() 进行了检查。
我 运行 在 $appointments= ..... -> get():
遇到错误"Object of class stdClass could not be converted to string"
我知道 get() 通常 returns 一个 stdClass 对象,但我不明白为什么我根本不能使用 get()。许多其他解决方案说使用 get() -> toArray();但是我什至无法将 get() 正确地设置为 运行 ..如果我不对结果进行 运行 get() 和 运行 dd()有关查询的信息,但不是结果本身。
我是Laravel/PHP的新手,所以我可能缺乏知识,但我希望有人能帮助我。
感谢您的宝贵时间!
您可以尝试使用 pluck()
检索 id 值数组,即:
$id = DB::table('accounts')
->select('id')
->where('account_status','Approved')
->where('demo','0')
->pluck('id');
使用 get()
,您将在 $id 变量中检索标准对象的集合。
您 运行 进入该错误与 get 方法无关。它是关于你试图对该查询的结果做什么。如果您尝试回显 collection class 的结果,它会抛出该错误。你不能回显一个对象吧?
根据文档:
get() return是一个集合对象。这意味着它有一系列的结果。在您的情况下,它 return 集合对象包含任何对查询进行数学计算的记录 ID
first() return只有一个结果包含一条记录
如果你只想要一个 id 到 return 并且可以被视为 string ,这样你就可以在你的视图中回显它,你必须改用 first() 方法。
结论:
如果需要,可以使用 get(),并且必须对每个结果进行 foraech 循环。 如果你想要,可以只使用一个 id first();
但请注意,即使您首先使用它 returns std class,所以在这种情况下,如果您想访问 id。你必须写 $result->id
并回显它。
在您的情况下,$id
是匹配数据的整行,您只需要 id 列,因此您需要这样做
$id = DB::table('accounts')
->select('id')
->where('account_status','Approved')
->where('demo','0')
->get();
//The ID column
$id = $id->id;