29.07.2016 Views

laravel-5

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Database: Query Builder 411<br />

.<br />

1 DB::table('users')<br />

2 ->where('id', 1)<br />

3 ->update(['votes' => 1]);<br />

Increment / Decrement<br />

The query builder also provides convenient methods for incrementing or decrementing the value of<br />

a given column. This is simply a short-cut, providing a more expressive and terse interface compared<br />

to manually writing the update statement.<br />

Both of these methods accept at least one argument: the column to modify. A second argument<br />

may optionally be passed to control the amount by which the column should be incremented /<br />

decremented.<br />

.<br />

1 DB::table('users')->increment('votes');<br />

2<br />

3 DB::table('users')->increment('votes', 5);<br />

4<br />

5 DB::table('users')->decrement('votes');<br />

6<br />

7 DB::table('users')->decrement('votes', 5);<br />

You may also specify additional columns to update during the operation:<br />

.<br />

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

Deletes<br />

Of course, the query builder may also be used to delete records from the table via the delete method:<br />

.<br />

1 DB::table('users')->delete();<br />

You may constrain delete statements by adding where clauses before calling the delete method:

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!