Skip to content

Commit 67afde3

Browse files
committed
working on wording
1 parent 6b7b671 commit 67afde3

File tree

1 file changed

+8
-6
lines changed

1 file changed

+8
-6
lines changed

queries.md

+8-6
Original file line numberDiff line numberDiff line change
@@ -381,18 +381,20 @@ To limit the number of results returned from the query, or to skip a given numbe
381381
$users = DB::table('users')->skip(10)->take(5)->get();
382382

383383
<a name="conditional-statements"></a>
384-
## Conditional Statments
384+
## Conditional Statements
385385

386-
In certain situations you may only want certain statements to apply to a query when something else is true. For instance you may only want to apply an `orderBy` statement if the url specified a column to sort on. To do that you would use the `when` method on your query:
386+
Sometimes you may want statements 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:
387+
388+
$role = $request->input('role');
387389

388390
$users = DB::table('users')
389-
->when(request()->input('role'), function($query) {
390-
return $query->where('role_id', request()->input('role'));
391+
->when($role, function ($query) {
392+
return $query->where('role_id', $role);
391393
})
392394
->get();
393-
394395

395-
The `when` method only applies the changes in the closure when the first parameter evaluates to true. If it evaluates to false, the query continues on without those changes like they don't exist.
396+
397+
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.
396398

397399
<a name="inserts"></a>
398400
## Inserts

0 commit comments

Comments
 (0)