Querying-related models in laravel 5.1

Let’s suppose that we are searching for the document number of a specific user. We will use the User and IdentityDocument entities we just saw. For the purpose of this example, imagine that you have a table identitydocuments with the following columns:

  • Number: This indicates the document number
  • Type: This indicates the document type
  • due_date: This indicates the due date of the document
  • City: This indicates the city where the document was released

Here’s the code to get the document identity number, starting from a User instance:

  $user = AppUser::where('first_name', '=', 'Francesco')->where('last_name', '=', 'Malatesta')->first();

  $identityDocumentNumber = $user->identityDocument->number;

If you echo $identityDocumentNumber, you will read the desired information. Nice, huh?

Recommended :  How to set Set trusted proxies for Laravel

Well, this is the way Laravel and Eloquent deal with querying your relationships. Once you have defined it, all you have to do is to access it as a simple property or a method.

All the other queries will be executed automatically by Laravel. In fact, follow these simple instructions:

  $user = AppUser::where('first_name', '=', 'Francesco')->where('last_name', '=', 'Malatesta')->first();

  $identityDocumentNumber = $user->identityDocument->number;

You just executed these queries:

  // the user Francesco Malatesta as an ID = 1...
  select * from users where first_name = 'Francesco' AND last_name = 'Malatesta';

  select * from identitydocuments where user_id = 1

Now put the result in to the $identityDocumentNumber. It is quite obvious for a one-to-one relationship; however, the same applies for a one-to-many relationship.

Recommended :  How To Embed media in HTML5 in easy way

Let’s consider another example right now: good old Jules will be a perfect fit. Let’s suppose that we want to get a list of every Jules Verne books we have, and the code is as follows:

  $author = AppAuthor::where('first_name', '=', 'Jules')->where('last_name', '=', 'Verne')->first();

  foreach($author->books as $book)
  {
    echo $book->title . <br/>;
  }

  // outputs:
  //
  // Journey to the Center of the Earth
  // Twenty Thousand Leagues Under the Sea
  // Around the World in Eighty Days
  // Michel Strogoff

Note

As I told you before, you can have access to your relationship using a simple attribute or a method call. What’s the difference? Well, with the method call, you can do some filtering, and everything you saw before, in order to get the desired result. In fact, you can raise a query on a relationship.

Recommended :  Learn how to import data from csv using Eloquent in Laravel

Imagine that we want to get all the books with a the in the title. Here’s the code:

  $author = AppAuthor::where('first_name', '=', 'Jules')->where('last_name', '=', 'Verne')->first();

  $theBooks = $author->books()->where('title', 'LIKE', '%the%')->get();

  foreach($theBooks as $book)
  {
    echo $book->title . <br/>;
  }

  // outputs:
  //
  // Journey to the Center of the Earth
  // Twenty Thousand Leagues Under the Sea
  // Around the World in Eighty Days

Cool, right? It’s not over yet, this is just scratching the surface!

About the author

Deven Rathore

I'm Deven Rathore, a multidisciplinary & self-taught designer with 3 years of experience. I'm passionate about technology, music, coffee, traveling and everything visually stimulating. Constantly learning and experiencing new things.

Pin It on Pinterest

Shares