Skip to content

Commit

Permalink
Updates ordering-grouping-limit-and-offset.md
Browse files Browse the repository at this point in the history
Auto commit by GitBook Editor
  • Loading branch information
BluewaterSolutions committed Aug 15, 2017
1 parent 1754e49 commit 2af1106
Show file tree
Hide file tree
Showing 7 changed files with 39 additions and 12 deletions.
8 changes: 6 additions & 2 deletions conditional-clauses.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
## Conditional Clauses

Sometimes you may want clauses to apply to a query only when something else is true. For instance you may only want to apply a `where` statement if a given input value is present on the incoming request. You may accomplish this using the `when` method:
##### LARAVEL PHP EQUIVELANT
```
$role = $request->input('role');
Expand All @@ -9,11 +11,13 @@ $users = DB::table('users')
return $query->where('role_id', $role);
})
->get();

```

The `when` method only executes the given Closure when the first parameter is `true`. If the first parameter is `false`, the Closure will not be executed.

You may pass another Closure as the third parameter to the `when` method. This Closure will execute if the first parameter evaluates as `false`. To illustrate how this feature may be used, we will use it to configure the default sorting of a query:
##### LARAVEL PHP EQUIVELANT
```
$sortBy = null;
Expand All @@ -24,5 +28,5 @@ return $query->orderBy($sortBy);
return $query->orderBy('name');
})
->get();

```

7 changes: 6 additions & 1 deletion deletes.md
Original file line number Diff line number Diff line change
@@ -1,12 +1,17 @@
## Deletes

The query builder may also be used to delete records from the table via the `delete` method. You may constrain `delete` statements by adding `where` clauses before calling the `delete` method:

##### LARAVEL PHP EQUIVELANT
```
DB::table('users')->delete();
DB::table('users')->where('votes', '>', 100)->delete();
```

If you wish to truncate the entire table, which will remove all rows and reset the auto-incrementing ID to zero, you may use the `truncate` method:

##### LARAVEL PHP EQUIVELANT
```
DB::table('users')->truncate();
```

7 changes: 5 additions & 2 deletions increment-and-decrement.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,18 @@ The query builder also provides convenient methods for incrementing or decrement

Both of these methods accept at least one argument: the column to modify. A second argument may optionally be passed to control the amount by which the column should be incremented or decremented:

##### LARAVEL PHP EQUIVELANT
```
DB::table('users')->increment('votes');
DB::table('users')->increment('votes', 5);
DB::table('users')->decrement('votes');
DB::table('users')->decrement('votes', 5);

```
You may also specify additional columns to update during the operation:

```
DB::table('users')->increment('votes', 1, ['name' => 'John']);

```
13 changes: 9 additions & 4 deletions inserts.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,29 @@

The query builder also provides an `insert` method for inserting records into the database table. The `insert` method accepts an array of column names and values:

##### LARAVEL PHP EQUIVELANT
```
DB::table('users')->insert(
['email' => 'john@example.com', 'votes' => 0]
);

```
You may even insert several records into the table with a single call to `insert` by passing an array of arrays. Each array represents a row to be inserted into the table:

##### LARAVEL PHP EQUIVELANT
```
DB::table('users')->insert([
['email' => 'taylor@example.com', 'votes' => 0],
['email' => 'dayle@example.com', 'votes' => 0]
]);

```
#### Auto-Incrementing IDs

If the table has an auto-incrementing id, use the `insertGetId` method to insert a record and then retrieve the ID:

##### LARAVEL PHP EQUIVELANT
```
$id = DB::table('users')->insertGetId(
['email' => 'john@example.com', 'votes' => 0]
);

> {note} When using PostgreSQL the insertGetId method expects the auto-incrementing column to be named `id`. If you would like to retrieve the ID from a different "sequence", you may pass the sequence name as the second parameter to the `insertGetId` method.
```

3 changes: 3 additions & 0 deletions ordering-grouping-limit-and-offset.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,13 @@
#### orderBy

The `orderBy` method allows you to sort the result of the query by a given column. The first argument to the `orderBy` method should be the column you wish to sort by, while the second argument controls the direction of the sort and may be either `asc` or `desc`:
##### LARAVEL PHP EQUIVELANT
```
$users = DB::table('users')
->orderBy('name', 'desc')
->get();
```

#### latest / oldest

Expand Down
9 changes: 7 additions & 2 deletions pessimistic-locking.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,13 @@

The query builder also includes a few functions to help you do "pessimistic locking" on your `select` statements. To run the statement with a "shared lock", you may use the `sharedLock` method on a query. A shared lock prevents the selected rows from being modified until your transaction commits:

##### LARAVEL PHP EQUIVELANT
```
DB::table('users')->where('votes', '>', 100)->sharedLock()->get();

```
Alternatively, you may use the `lockForUpdate` method. A "for update" lock prevents the rows from being modified or from being selected with another shared lock:

DB::table('users')->where('votes', '>', 100)->lockForUpdate()->get();
##### LARAVEL PHP EQUIVELANT
```
DB::table('users')->where('votes', '>', 100)->lockForUpdate()->get();
```
4 changes: 3 additions & 1 deletion updates.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@

Of course, 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 containing the columns to be updated. You may constrain the `update` query using `where` clauses:

##### LARAVEL PHP EQUIVELANT
```
DB::table('users')
->where('id', 1)
->update(['votes' => 1]);

```

0 comments on commit 2af1106

Please sign in to comment.