O'Reilly logo

Learning Laravel's Eloquent by Francesco Malatesta

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Querying-related models

Now that you have learned how to define your relationships, I think you are ready to learn how to query them. Let's start with a very basic example.

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 = \App\User::where('first_name', '=', 'Francesco')->where('last_name', ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required