Database: Query Builder
Introduction
Kiaan's database query builder provides a convenient, fluent interface to creating and running database queries. It can be used to perform most database operations in your application and works perfectly with all of Kiaan's supported database systems.
The Kiaan query builder uses PDO parameter binding to protect your application against SQL injection attacks.
There is no need to clean or sanitize strings passed to the query builder as query bindings.
Methods
Table
You may use the table
method to select a table:
and you can use array parameter:
Retrieving All Rows From A Table
You may use the table
method provided by the DB
facade to begin a query. The table
method returns a fluent query builder instance for the given table, allowing you to chain more constraints onto the query and then finally retrieve the results of the query using the $user = DB::table('users')->where('name', 'Hassan')->first();
return $user->email; method:
To retrieve all data, use the data
method:
Retrieving A Single Row
If you just need to retrieve a single row from a database table, you may use the DB
facade's first
method. This method will return a single stdClass
object:
If you don't need an entire row, you may extract a single value from a record using the value
method. This method will return the value of the column directly:
To retrieve a single row by its id
column value, use the find
method:
to change id key , use the key
method:
Aggregates
The query builder also provides a variety of methods for retrieving aggregate values like count
, max
, min
, avg
, and sum
. You may call any of these methods after constructing your query:
Determining If Records Exist
Instead of using the count
method to determine if any records exist that match your query's constraints, you may use the exists
method:
Select Statements
You may not always want to select all columns from a database table.
Using the select
method, you can specify a custom "select" clause for the query:
The distinct
method allows you to force the query to return distinct results:
Joins
You can use this method in 7 ways. These;
join
leftJoin
rightJoin
innerJoin
fullOuterJoin
leftOuterJoin
rightOuterJoin
Examples:
Where Clauses
You can use this method in 6 ways. These;
where
orWhere
notWhere
orNotWhere
whereNull
whereNotNull
Where Clauses
You may use the query builder's where
method to add "where" clauses to the query. The most basic call to the where
method requires three arguments. The first argument is the name of the column. The second argument is an operator, which can be any of the database's supported operators. The third argument is the value to compare against the column's value.
For example, the following query retrieves users where the value of the votes
column is equal to 100
and the value of the age
column is greater than 35
:
For convenience, if you want to verify that a column is =
to a given value, you may pass the value as the second argument to the where
method. Kiaan will assume you would like to use the =
operator:
and other example:
You may also pass an array of conditions to the where
function. Each element of the array should be an array containing the three arguments typically passed to the where
method:
Or Where Clauses
When chaining together calls to the query builder's where
method, the "where" clauses will be joined together using the and
operator. However, you may use the orWhere
method to join a clause to the query using the or
operator. The orWhere
method accepts the same arguments as the where
method:
If you need to group an "or" condition within parentheses, you may pass a closure as the first argument to the grouped method:
Where Not Clauses
The whereNot
and orWhereNot
methods may be used to negate a given group of query constraints. For example, the following query excludes products that are on clearance or which have a price that is less than ten:
Additional Where Clauses
whereNull / whereNotNull
The whereNull
method verifies that the value of the given column is NULL
:
The whereNotNull
method verifies that the column's value is not NULL
:
whereId
When id is equal to a value.
In
You can use this method in 4 ways. These;
in
orIn
notIn
orNotIn
The in method verifies that a given column's value is contained within the given array:
The notIn
method verifies that the given column's value is not contained in the given array:
findInSet
You can use this method in 4 ways. These;
findInSet
orFindInSet
notFindInSet
orNotFindInSet
Example:
Between
between / orBetween
The between
method verifies that a column's value is between two values:
notBetween / orNotBetween
The notBetween
method verifies that a column's value lies outside of two values:
Like
You can use like method for search.
You can use this method in 4 ways. These;
like
orLike
notLike
orNotLike
Example:
Grouping
The groupBy & having Methods
As you might expect, the groupBy
and having
methods may be used to group the query results. The having
method's signature is similar to that of the where
method:
You can use the having
method to filter the results:
orderBy
The orderBy method is used to sort the result-set in ascending or descending order.
Limit & Offset
You may use the limit
and offset
methods to limit the number of results returned from the query or to skip a given number of results in the query:
Insert Statements
The query builder also provides an insert
method that may be used to insert records into the database table. The insert
method accepts an array of column names and values:
You may insert several records at once by passing an array of arrays. Each array represents a record that should be inserted into the table:
Auto-Incrementing IDs
If the table has an auto-incrementing id, use the lastId
method to get a last insert ID:
Or
Update Statements
In addition to inserting records into the database, the query builder can also update existing records using the update
method. The update
method, like the insert
method, accepts an array of column and value pairs indicating the columns to be updated. The update
method returns the number of affected rows. You may constrain the update
query using where
clauses:
Find Or Fail
Takes an id and returns a single model. If no matching model exist, it throws an error.
First or Fail
Returns the first record found in the database. If no matching model exist, it throws an error
First Or
Sometime, you need to write logic for default value. when you find some product from database and it's not match any record then you can return default product. so you have to write long logic behind this but eloquent provide firstOr() where you can easily return default object.
First Or Insert
It tries to find a model matching the attributes you pass in the first parameter. If a model is not found, it automatically creates and saves a new Model after applying any attributes passed in the second parameter:
Update Or Insert
Sometimes you may want to update an existing record in the database or create it if no matching record exists. In this scenario, the updateOrInsert
method may be used. The updateOrInsert
method accepts two arguments: an array of conditions by which to find the record, and an array of column and value pairs indicating the columns to be updated.
The updateOrInsert
method will attempt to locate a matching database record using the first argument's column and value pairs. If the record exists, it will be updated with the values in the second argument. If the record can not be found, a new record will be inserted with the merged attributes of both arguments:
Increment & Decrement
The query builder also provides convenient methods for incrementing or decrementing the value of a given column. Both of these methods accept at least one argument: the column to modify. A second argument may be provided to specify the amount by which the column should be incremented or decremented:
Delete
The query builder's delete
method may be used to delete records from the table. The delete
method returns the number of affected rows. You may constrain delete
statements by adding "where" clauses before calling the delete
method:
Auto Increment
Get next auto increment number for table.
Query execute
To query execute use query method:
Last updated