-
-
Notifications
You must be signed in to change notification settings - Fork 0
Query Builder
- consistentRead
- condition
- decrement
- delete
- fetch
- from
- filter
- first
- find
- increment
- insert
- insertOrReplace
- whereKey
- whereKeyBetween
- whereKeyBeginsWith
- key
- limit
- putItem
- select
- scanFromBackward
- orFilter
- raw
- query
- scan
- update
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 scan
method:
namespace App\Http\Controllers;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\DB;
class UserController extends Controller
{
/**
* Show a list of all of the application's users.
*
* @return \Illuminate\Http\Response
*/
public function index()
{
// Scan operation
$users = DB::table('users')->scan();
// Query Operation
$users = DB::table('users')->keyCondtion('PK', 'USERS')->query();
return view('user.index', ['users' => $users]);
}
}
Determines the read consistency model: If set to true, then the operation uses strongly consistent reads; otherwise, the operation uses eventually consistent reads.
Strongly consistent reads are not supported on global secondary indexes. If you query a global secondary index with ConsistentRead set to true
, you will receive a ValidationException
.
Type: Boolean
DB::table('users')->consistentRead(true)->keyCondtion('PK', 'USERS')->query();
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:
DB::table('users')->key(['id' => 'id-1'])->increment('votes');
DB::table('users')->key(['id' => 'id-1'])->increment('votes', 5);
DB::table('users')->key(['id' => 'id-1'])->decrement('votes');
DB::table('users')->key(['id' => 'id-1'])->decrement('votes', 5);
You may also specify additional columns to update during the operation:
DB::table('users')->key(['id' => 'id-1'])->increment('votes', 1, ['name' => 'John']);
You may also specify add
or set
mode, by default increment or decrement, is using set
mode. But you can add the prefix add:
to use add
mode.
DB::table('users')->key(['id' => 'id-1'])->decrement('add:votes', 5);
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:
DB::table('users')->insert([
'email' => 'kayla@example.com',
'votes' => 0
]);
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. You may constrain the update query using key
method:
DB::table('users')->key(['id' => 'id-1'])->update([
'votes' => 5
]);
The query builder's delete
method may be used to delete records from the table. You may constrain delete statements by adding "key" method before calling the delete method:
DB::table('users')->key(['id' => 'id-1'])->delete();