If you want to learn more about SQL in general make sure to check out this free introduction to SQL eBook here. If you are just getting started with Laravel, make sure to check out this introduction course here: However, I would personally first check if there are any entires that match the condition and then get the property. Now we see find and findOrFail method input parameter one parameter. So here i will show the difference between find and findOrFail method. So from here you can find a method in add parameter to get a records. If you wanted to get the value of a single column, you could use the following: User::where( 'username', 'bobbyiliev')->first()->name Here you can require to find a record in the database table for by using find and findOrFail method. You could do the same with the first() and all() methods as well: $user = User::where( 'username', 'bobbyiliev')->first() The argument needs to be an array containing a list of the columns that you want to get: $user = User::where( 'username', 'bobbyiliev')->get() However, if you wanted to get only a specific column, you could pass it as an argument to the get() method. To get all of the columns from the users table, we would use the following: $user = User::where( 'username', 'bobbyiliev')->get() Let's say that we already have our User model in place. Select specific columns with Laravel Eloquent Now that we know how to do this with SQL only, let's learn how we can achieve this with Laravel. If you wanted to get two columns, you would divide them by a comma: SELECT name,age FROM users WHERE username = 'bobbyiliev' However, if you wanted to get only, let's say, the age of the user, you would need to change the * symbol with the name of the column: SELECT age FROM users WHERE username = 'bobbyiliev' ![]() If you wanted to get all of the information from that table with only SQL the query would look like this: Checkout our latest product - the ultimate tailwindcss page creator □ SELECT * FROM users WHERE username = 'bobbyiliev' Let's say that we had a table called users with the following columns: name, username, age, email, phone. ![]() Or you could use this awesome script to do the installation: How to Install Laravel on DigitalOcean with 1-Click.If you do not have that yet, you can follow the steps from this tutorial on how to do that: If you wish, you can use my affiliate code to get free $100 DigitalOcean credit to spin up your own servers! I will be using a DigitalOcean Ubuntu Droplet for this demo. In this tutorial, you will learn how to select specific columns in Laravel eloquent! Prerequisitesīefore you start, you would need to have a Laravel application up and running. In some specific cases you might not want to get all of the columns from a specific table but just one or some of them. This simplifies all CRUD (Create, read, update, and delete) operations and any other database queries. The Eloquent ORM included with Laravel provides you with an easy way of interacting with your database. So in this case, we will have $user->posts_count and $user->comments_count variables.How to Select Specific Columns in Laravel Eloquent? ![]() $users = User::withCount()->get() Įvery parameter that is specified in withCount() method, becomes main object’s _count property. ![]() To do that, here’s our UserController code: public function index() Now, we will try to show this table – the list of users and the amount of their posts/comments: Return $this->hasManyThrough(Comment::class, Post::class) All relationships can be described, looking at app/User.php model: public function posts() In our example mini-project, we will have three models: User, Post and Comment. It also works with two layers deep, inside of hasManyThrough relations. Eloquent has one less-known function called withCount(): it helps to get the amount of related records inside of the main object.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |