从同一表获取不同的数据

发布于 2025-02-03 08:06:59 字数 990 浏览 2 评论 0原文

我有一个名为用户的表。我已经将用户和专营权存储在同一张表中。在存储用户时,我也在与该行的同一张表中存储了特许经营ID。因此,如何获取特许经营信息以及用户信息... ???

 return  $data= DB::table('users')
            ->join('tbl_customergroups', 'tbl_customergroups.customergroup_id', 'users.group_id')
            ->where('users.user_type', 2)
            ->where('users.id', 'users.franchise_id')
            ->where('users.status', 0)
            ->orWhere('users.user_type', 5)
            ->select('tbl_customergroups.*', 'users.*')
            ->latest('users.created_at')
            ->paginate(10);

用户的表模式:

            $table->string('franchise_id')->nullable();
            $table->string('user_id')->nullable();
            $table->string('date_of_join')->nullable();
            $table->string('franchise_id')->nullable();
            $table->string('name')->nullable();
            $table->string('email')->unique(); ```

I have a table named users. I have stored users and the franchise in the same table. and while storing users I have stored franchise id as well in that same table with the row. so how can I fetch franchise information along with the user information...???

 return  $data= DB::table('users')
            ->join('tbl_customergroups', 'tbl_customergroups.customergroup_id', 'users.group_id')
            ->where('users.user_type', 2)
            ->where('users.id', 'users.franchise_id')
            ->where('users.status', 0)
            ->orWhere('users.user_type', 5)
            ->select('tbl_customergroups.*', 'users.*')
            ->latest('users.created_at')
            ->paginate(10);

User's table schema :

            $table->string('franchise_id')->nullable();
            $table->string('user_id')->nullable();
            $table->string('date_of_join')->nullable();
            $table->string('franchise_id')->nullable();
            $table->string('name')->nullable();
            $table->string('email')->unique(); ```

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

鲜肉鲜肉永远不皱 2025-02-10 08:06:59

如果您想要雄辩解决方案,我假设用户模型与特许经营模型之间存在关系,这可能是一个可能的解决方案:

您的用户模型应该具有像这:

public function franchise(){
   return $this->hasOne(Franchise::class);
}

在您的控制器中,您必须使用这样的用户模型来获取专营权:

User::with('franchise')->get();

If you want Eloquent solutions, and I am assuming that there is a relation between the users model and the franchise model, this could be a possible solution:

Your User model should have a function like this:

public function franchise(){
   return $this->hasOne(Franchise::class);
}

In your controller you have to fetch the franchise using a user model like this:

User::with('franchise')->get();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文