加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

php – Laravel连接表

发布时间:2020-12-14 19:36:43 所属栏目:大数据 来源:网络整理
导读:我有2个表,我需要加入这些表. 我需要从库中选择id和name,其中share gal.id = galleries.id和user_id = auth :: id(). 表: Galleries: id,nameShare: gal_id,user_id 请给我看一下laravel的例子.我需要显示它. 解决方法 为实现这一目标,您在Eloquent ORM中拥
我有2个表,我需要加入这些表.
我需要从库中选择id和name,其中share gal.id = galleries.id和user_id = auth :: id().

表:

Galleries: id,name
Share: gal_id,user_id

请给我看一下laravel的例子.我需要显示它.

解决方法

为实现这一目标,您在Eloquent ORM中拥有关系.官方网站声明:

Of course,your database tables are probably related to one another. For example,a blog post may have many comments,or an order could be related to the user who placed it. Eloquent makes managing and working with these relationships easy. Laravel supports many types of relationships:

你可以阅读有关here的雄辩关系的所有信息

如果您不想使用关系,以下是如何连接两个表的示例:

DB::table('users')
->select('users.id','users.name','profiles.photo')
->join('profiles','profiles.id','=','users.id')
->where(['something' => 'something','otherThing' => 'otherThing'])
->get();

上面的查询将基于user.id = profile.id以及两个不同的条件连接两个表,即用户和配置文件,您可能会也可能不会使用where子句,这完全取决于您要实现的目标.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读