SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    SitePoint Addict bronze trophy
    Join Date
    Sep 2005
    Posts
    323
    Mentioned
    5 Post(s)
    Tagged
    0 Thread(s)

    Laravel Join Table on Auth/User table?

    So I have a table that is related to my user table; let's call this user_address.

    I want to join the user_address table onto my User table like

    INNER JOIN user_address ON user_address.userid = user.ID

    But I was wondering how to do this using the Auth Class/User Model in Laravel? I could add a relationship to the User Model class for the user_address, but that feels like it's causing another query because the user_address is always needed.

    Instead of running Auth::user()->user_address->street_name I'd prefer to just do Auth::user()->street_name... but I'm unsure where I can perform the join in the Auth/User model classes.

    Anyone have any information on this type of situation?

    Thanks

  2. #2
    SitePoint Guru
    Join Date
    Nov 2003
    Location
    Huntsville AL
    Posts
    701
    Mentioned
    4 Post(s)
    Tagged
    1 Thread(s)
    Read through this: http://laravel.com/docs/eloquent

    Especially the section on eager loading.

  3. #3
    SitePoint Addict bronze trophy
    Join Date
    Sep 2005
    Posts
    323
    Mentioned
    5 Post(s)
    Tagged
    0 Thread(s)
    Hi thanks for the reply;

    That's still using two queries when I'm pretty sure I can perform this just using one query with a simple join on my user table; but I don't know how to make the User model join automatically when it's instantiated.

  4. #4
    SitePoint Guru
    Join Date
    Nov 2003
    Location
    Huntsville AL
    Posts
    701
    Mentioned
    4 Post(s)
    Tagged
    1 Thread(s)
    Are you sure? I don't have eloquent setup myself but the whole point behind eager loading is to reduce the number of queries. The documentation sure seems to indicate that a join will be performed.

    Not that it will make any difference anyways for your use case. Extra queries are seldom a problem. If your application reaches the "millions of requests per second" stage then maybe you can do some optimization.

    You might consider checking the forum that overflows with questions. They have dozens of experienced Laravel developers. I like sitepoint but it's not so good with these sorts of questions.

  5. #5
    SitePoint Addict bronze trophy
    Join Date
    Sep 2005
    Posts
    323
    Mentioned
    5 Post(s)
    Tagged
    0 Thread(s)
    I think in the documentation it says two queries would be performed.

    I will take a look at the forums and see if I can get some answers there, Thanks!

  6. #6
    SitePoint Wizard silver trophybronze trophy asp_funda's Avatar
    Join Date
    Jun 2003
    Location
    ether
    Posts
    4,497
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Eager loading will run 2 SELECT queries but thats much more faster and less expensive than a join. So you have to weigh in performance over personal comfort. Speaking of personal comfort, you won't be able to do the join you want straight up. You will need to add a query scope in your User model to make the join and fetch results and you will need to extend the Auth class to create your own which uses that query scope to load up the data.
    Our lives teach us who we are.
    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    Me - Photo Blog - Personal Blog - Dev Blog
    iG:Syntax Hiliter -- Colourize your code in WordPress!!


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •