06.10.2016 Views

laravel-5

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Database: Query Builder 522<br />

1 $titles = DB::table('roles')->pluck('title');<br />

2<br />

3 foreach ($titles as $title) {<br />

4 echo $title;<br />

5 }<br />

You may also specify a custom key column for the returned array:<br />

1 $roles = DB::table('roles')->pluck('title', 'name');<br />

2<br />

3 foreach ($roles as $name => $title) {<br />

4 echo $title;<br />

5 }<br />

Aggregates<br />

The query builder also provides a variety of aggregate methods, such as count, max, min, avg, and<br />

sum. You may call any of these methods after constructing your query:<br />

1 $users = DB::table('users')->count();<br />

2<br />

3 $price = DB::table('orders')->max('price');<br />

Of course, you may combine these methods with other clauses to build your query:<br />

1 $price = DB::table('orders')<br />

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

3 ->avg('price');

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

Saved successfully!

Ooh no, something went wrong!