【问题标题】:Object of class stdClass could not be converted to string in laravel 5.5在 laravel 5.5 中,stdClass 类的对象无法转换为字符串
【发布时间】:2017-09-25 17:38:28
【问题描述】:

我有两个名为“用户”和“赞助商”的表格 - 第一个是“用户”,第二个是“赞助商”

我正在尝试从“用户”表中获取用户详细信息,其中“赞助商”表中的所有用户都使用另一个用户“用户名”作为他们的赞助商。我写了类似下面的东西——当试图加入表时得到这个错误。什么应该是正确的查询?请有人帮助我。

$a = Sentinel::getUser()->user_name;
                $m = DB::table('sponsors')
                        ->select('user_name')
                        ->where('sponsor', '=', $a)
                        ->get();
                //dd($m);
                $second_downline_members = DB::table('sponsors')
                                            ->join('users', 'users.id', '=', 'sponsors.user_id')
                                            ->where('users.id', $m)
                                            ->get();
                dd($second_downline_members);

【问题讨论】:

  • $m 是集合,您试图将其作为字符串传递到 ->where('users.id', $m)
  • 那么正确的查询应该是什么?
  • 提供了一个为用户获得赞助商的答案,如果有帮助,您可以尝试。
  • 确保您有一些当前用户的赞助商,并检查表格和字段的拼写。
  • 我已经检查过了,我当前的用户名 = 'rashed' 和其他四个用户名使用 'rashed' 作为他们的赞助商。所以,我需要“用户”表中这四个“用户名”详细信息的详细信息

标签: php mysql laravel


【解决方案1】:
$username = Sentinel::getUser()->user_name;
        $user_ids = \DB::table('sponsors')
            ->where('sponsor', $username)
            ->pluck('user_id');

        $users = \DB::table('users')->whereIn('id', $user_ids)->get();

        dd($users);

【讨论】:

  • 它没有返回任何项目。
  • 检查 $a 的值是 id 吗?
  • 我刚试过我自己这是一个 100% 工作正常的代码 $id = Auth::user()->id; $articles = DB::table('articles') ->join('users', 'users.id', '=', 'articles.user_id') ->where('users.id', $id) ->get();
  • 您正在获取所有用户,但我需要获取所有用户,他们使用与赞助商相同的 user_name。
  • 让我为你修改一下
【解决方案2】:
$a = Sentinel::getUser()->user_name;
                $m = DB::table('sponsors')
                        ->select('user_name')
                        ->where('sponsor', '=', $a)
                        ->first();
                //dd($m);
                $second_downline_members = DB::table('sponsors')
                                            ->join('users', 'users.id', '=', 'sponsors.user_id')
                                            ->where('users.id', $m->id)
                                            ->get();
                dd($second_downline_members);

试试这个

【讨论】:

  • 现在收到此错误-“此集合实例上不存在属性 [id]”。还尝试了 ->where('users.user_name', $m->user_name),它说“此集合实例上不存在属性 [user_name]”。但是这个集合中确实存在“user_name”。
  • 你在 $m 查询中是否将 get 更改为 first
  • 它只提供第一个用户详细信息。许多用户可以使用相同的赞助商,所以我需要获取所有使用相同赞助商的用户详细信息。
【解决方案3】:

将您的选择更改为

->select('user_name', 'id')

【讨论】:

    猜你喜欢
    • 2020-03-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-05-28
    • 1970-01-01
    • 2017-09-21
    • 2011-04-06
    相关资源
    最近更新 更多