gpt4 book ai didi

laravel - 从 Laravel 的一行中选择单列?

转载 作者:行者123 更新时间:2023-12-02 11:23:47 26 4
gpt4 key购买 nike

如何从 Laravel 4 的单行中获取单列?

这是我正在尝试的:

return DB::selectOne("
SELECT EXISTS(
SELECT *
FROM permissions p
JOIN role_permissions rp ON rp.permission_id=p.id
JOIN user_roles ur ON ur.role_id=rp.role_id
WHERE ur.user_id=? AND p.code=?
)
",[Auth::user()->id,$perm_code]);
selectOne虽然返回这个丑陋的对象:
object(stdClass)#297 (1) {
["EXISTS(
SELECT *
FROM permissions p
JOIN role_permissions rp ON rp.permission_id=p.id
JOIN user_roles ur ON ur.role_id=rp.role_id
WHERE ur.user_id=? AND p.code=?
"]=>
string(1) "0"
}

有什么更好的吗?

使用 PDO 也很丑陋:
$stmt = DB::getPdo()->prepare("
SELECT EXISTS(
SELECT *
FROM permissions p
JOIN role_permissions rp ON rp.permission_id=p.id
JOIN user_roles ur ON ur.role_id=rp.role_id
WHERE ur.user_id=? AND p.code=?
)
");
$stmt->execute([Auth::user()->id,$perm_code]);
return (bool)$stmt->fetchColumn();

最佳答案

用这个:

DB::table('users')->pluck('columname');

从内联文档:
/**
* Pluck a single column's value from the first result of a query.
*
* @param string $column
* @return mixed
*/

关于laravel - 从 Laravel 的一行中选择单列?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23586452/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com