0
我有2個表,我需要加入這些表。 我需要select
id
和nam
e從galleries
,其中分享gal.id = galleries.id
和user_id = auth::id()
。Laravel加入表
表:
Galleries: id, name
Share: gal_id, user_id
請出示例如laravel。我需要顯示它。
我有2個表,我需要加入這些表。 我需要select
id
和nam
e從galleries
,其中分享gal.id = galleries.id
和user_id = auth::id()
。Laravel加入表
表:
Galleries: id, name
Share: gal_id, user_id
請出示例如laravel。我需要顯示它。
爲了達到這個目的,您在Eloquent ORM中擁有關係。官方網站聲明:
當然,您的數據庫表可能是相互關聯的。例如,博客帖子可能有很多評論,或者訂單可能與放置它的用戶有關。雄辯使管理和處理這些關係變得容易。 Laravel支持多種類型的關係:
你可以閱讀所有關於在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和兩個不同的條件加入兩個表,即users
和profiles
,您可能會也可能不會使用where
子句,這完全取決於您正在努力實現。
請發佈表中的sql以及您用來訪問它的代碼。 – rapvelopment 2014-12-13 13:49:00