Tutorials , code & Inspiration

Using Utility methods in laravel

As you probably imagined, Eloquent has a lot of utilities and methods that can improve your life as a developer. Trying to cover every single method would be hard but besides the most common methods, there are a few other methods that deserve to be here.

First of all, the skip() and take() methods that are used to implement some pagination in your queries.

  $books = AppBook::skip(10)->take(10)->get();

We are telling Eloquent to take 10 records and skip 10 from the start. So, if skip(0)->take(10) will take records from 1 to 10, skip(10)->take(10) will take from 11 to 20, and so on.

Of course, you can’t also miss the orderBy, groupBy, and having methods. If you know a little about SQL databases (and if you are here, I think you do) you won’t have any problem understanding this code:

  // orderBy
  AppBook::orderBy('title', 'asc')->get();

  // groupBy

  // having
  AppBook::having('count', '<', 20)->get();


Get the best in web dev

Join dunebook.com and recieve best in web dev , once a week FREE

An email has been Sent to your Inbox ! Please Confirm your Subscription :)

Pin It on Pinterest