Thursday 15 August 2013

Simple SQL to Eloquent Query (Laravel) -


I have two tables: user (users) and group (group).

  User ----------------- ID Username Group 1 | Bob | 2 groups ----------------- ID Name 1 First Group 2 | Second group   

I would like to display: users.id, users.username, group.name (1, Bob, secondgroup) will act like a SQL statement:

 Select  Users.id, Users.username, Groups.name from the user INNER JOIN Group Group.id = Users.group   

However, I am struggling to write it in I'm eloquent, since there is no "FROM", I am going to JOINS () using something on the following lines.

  $ users = users :: select ('id', 'Username', 'groups.name') - & gt; ('Groups.id', '=', 'id') - & gt; get ();   

It is not working anymore - I think that has to be selected before joining, but I can not work it: (

I think you are confusing some things here ...

You can elite Low-level DB :: table ('foo') -> Selection () with syntax. I suggest you take a look at when you want to use lucrative .

If you like your model Should be defined by:

  Class User Expands {Public Function Group ()} Expands {Return $ this- & gt; Affiliate $ ('Group', 'Group'); / / The second parameter is required because you did not name the column "group_id", but only "group"}} extends class group elusive {public function user ()) {return $ this- & gt; There are many ('user', 'group');}}   

It connects all that you may need later. Then you can simply click the User :: with ('group') -> gt; All (); and you have to build and run a query.

No comments:

Post a Comment