1/17/2023 0 Comments Laravel eloquent![]() We often write queries against certain ‘where conditions’. Things get more interesting further down. Instead of returning the entire result array we can simply pluck out that specific column? $user_id = DB::table('users')->where('name', 'John')->pluck('id') įor specifying more than one column we can use the select clause $users = DB::table('users')->select('name', 'email')->get() What if we need only the user id of John. The first() method will only return the first find. Here, we are trying to fetch a row that has the value John in its name column. Similarly to fetch a single row we can modify the above code by adding a where clause $user = DB::table('users')->where('name', 'John')->first() The table from which the value has to be selected is mentioned inside the brackets within quotes and finally the get() method gets the values. ![]() So, how do we create a simple select query to fetch all values from the users table?ĭB::table is responsible to begin a fluent query against a database table. We can avoid all those lines of code to sanitize the data before feeding it to the DB. The notable factor about query builder is that, since it uses the PHP Data Objects (PDO), we need not worry about SQL injection attacks (Make sure we don’t inadvertently remove this protection). and it works on all supported database systems like a champ. It can be used to perform all the database operations in your application, from basic DB Connection, CRUD, Aggregates, etc. ![]() ![]() In Laravel the database query builder provides an easy interface to create and run database queries. Through this blog I intend to share few quick pointers on these concepts. A few tools that make Laravel an awesome framework is the inclusion of “Query Builder and Eloquent ORM”. Laravel’s Query Builder and Eloquent ORMĪn web application always needs to interact with a database and Laravel makes this task hassle free. ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |