php – Laravel连接表

前端之家收集整理的这篇文章主要介绍了php – Laravel连接表前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我有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子句,这完全取决于您要实现的目标.

猜你在找的Laravel相关文章