Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

Sorry, you do not have permission to ask a question, You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please type your username.

Please type your E-Mail.

Please choose an appropriate title for the post.

Please choose the appropriate section so your post can be easily searched.

Please choose suitable Keywords Ex: post, video.

Browse

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise

Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise Logo Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise Logo

Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise Navigation

  • Home
  • About Us
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • About Us
  • Contact Us
Home/ Questions/Q 4532

Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise Latest Questions

Author
  • 61k
Author
Asked: November 27, 20242024-11-27T12:50:08+00:00 2024-11-27T12:50:08+00:00

Laravel Eloquent Tutorial – Get only the fields (aka columns) we need from relation.

  • 61k

Laravel is a great framework to work with, and I use it every day for my 9-5 job. One of the greatest power of Laravel is, it has an ORM (Object Relational Mapping) as default called Eloquent.

I am not going to talk about what is Eloquent ORM, but I would like to just indicate that, it makes your life easier to deal with databases.

Below you can find some simple usages of eloquent;

$post = Post::find(1) // That will run the following query in the database // select * from posts where id = 1 // You see, it's so easy to use and powerful because we don't need to write a query anymore 
Enter fullscreen mode Exit fullscreen mode

If you are using relational databases such as MySQL, MariaDB, PostgreSQL and etc, it becomes more powerful because you don't need to write complex join queries anymore.

For example, we want to get a specific post from the posts table but also we want to GET the author of the post as well. The following example, clearly explains that.

class Post extends Model {  ....  // that will assume that we have author_id column in the posts table  public function author(): BelongsTo {     return $this->belongsTo(related: Author::class)  } }   // Since the author relation is defined in Post model, we can load it using following code $post = Post::with('author')->find(1); // Now, the post data will have the author data from database // This is called Eager loading and it is very important learn about it. // Link: https://laravel.com/docs/9.x/eloquent-relationships#eager-loading 
Enter fullscreen mode Exit fullscreen mode

Since we used eager loading to get author data with the post, all columns in the author data is loaded. This is not a problem with a small amount of data, but if we get millions of posts, each post will have an author and probably that will run a little bit slower. So, instead of getting all columns in author data, let's get some specific columns.

$post = Post::with('author:id, name')->find(1); // Now, the post data will have the author data from database but only specific columns id and name // This will make your it more faster to load data 
Enter fullscreen mode Exit fullscreen mode

In the example above, we have used :id, name, so, only those columns are going to be loaded from the database.

That's all for this tutorial 🙂

If you like to get more content similar to that, please follow me on Twitter and on my youtube channel.

Twitter: twitter.com/olgunsoftware

Youtube: youtube.com/channel/UCH-xplaz-cmloIUkYspcEhQ

laravelphpprogrammingwebdev
  • 0 0 Answers
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

Sidebar

Ask A Question

Stats

  • Questions 4k
  • Answers 0
  • Best Answers 0
  • Users 2k
  • Popular
  • Answers
  • Author

    How to ensure that all the routes on my Symfony ...

    • 0 Answers
  • Author

    Insights into Forms in Flask

    • 0 Answers
  • Author

    Kick Start Your Next Project With Holo Theme

    • 0 Answers

Top Members

Samantha Carter

Samantha Carter

  • 0 Questions
  • 20 Points
Begginer
Ella Lewis

Ella Lewis

  • 0 Questions
  • 20 Points
Begginer
Isaac Anderson

Isaac Anderson

  • 0 Questions
  • 20 Points
Begginer

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help

Footer

Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise

Querify Question Shop: Explore, ask, and connect. Join our vibrant Q&A community today!

About Us

  • About Us
  • Contact Us
  • All Users

Legal Stuff

  • Terms of Use
  • Privacy Policy
  • Cookie Policy

Help

  • Knowledge Base
  • Support

Follow

© 2022 Querify Question. All Rights Reserved

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.