29.07.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 408<br />

Exists Statements<br />

The whereExists method allows you to write where exist SQL clauses. The whereExists method<br />

accepts a Closure argument, which will receive a query builder instance allowing you to define the<br />

query that should be placed inside of the “exists” clause:<br />

.<br />

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

2 ->whereExists(function ($query) {<br />

3 $query->select(DB::raw(1))<br />

4 ->from('orders')<br />

5 ->whereRaw('orders.user_id = users.id');<br />

6 })<br />

7 ->get();<br />

The query above will produce the following SQL:<br />

.<br />

1 select * from users<br />

2 where exists (<br />

3 select 1 from orders where orders.user_id = users.id<br />

4 )<br />

Ordering, Grouping, Limit, & Offset<br />

orderBy<br />

The orderBy method allows you to sort the result of the query by a given column. The first argument<br />

to the orderBy method should be the column you wish to sort by, while the second argument controls<br />

the direction of the sort and may be either asc or desc:<br />

.<br />

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

2 ->orderBy('name', 'desc')<br />

3 ->get();

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

Saved successfully!

Ooh no, something went wrong!