Laravel连接表 [英] Laravel join tables

查看:240
本文介绍了Laravel连接表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有2个表,我需要加入这些表. 我需要从galleries中共享gal.id = galleries.iduser_id = auth::id()select idnam e.

I have 2 tables and I need to join those tables. I need to select id and name from galleries, where share gal.id = galleries.id and user_id = auth::id().

表格:

Galleries: id, name
Share: gal_id, user_id

请给我展示laravel中的示例.我需要显示它.

Please show me example in laravel. And I need to display it.

推荐答案

要实现此目的,您需要在雄辩的ORM中具有关系".官方网站上注明:

To achieve this, you have Relationship in Eloquent ORM. The official website states :

当然,您的数据库表可能相互关联.例如,博客文章可能有很多评论,或者订单可能与放置它的用户有关.口才好使管理和使用这些关系变得容易. Laravel支持许多类型的关系:

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:

您可以在此处

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

If you do not want to use relationship, following is an example on how to join two tables :

   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连接两个表,分别为usersprofiles,您是否可以使用where子句,这完全取决于您正在努力实现.

The above query will join two tables namely users and profiles on the basis of user.id = profile.id with two different conditions, you may or may not use where clause, that totally depends on what you are trying to achieve.

这篇关于Laravel连接表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆