Eloquent Builder

It provides a convenient, fluent interface to creating and running database queries

 

Selecting fields inside eager loading method "with"

Suppose there is a relation Post belongsTo User. If we are retrieving posts with associated user relation, we can use...


Quick way to save or update post with one liner of eloquent code

Typically, we fetch the post with a single or set of criteria to see if it already exists, and if...


Using model observer - a separate class to observe model changes and run desired logic

Model observer methods checks if any changes occur around model such as - created() after record getting creeated- creating() before...


Changing model values using mutators before saving in database

Sometimes we need to change model value before saving in the database. With help of mutators we can achieve this...


Using parameters in find() method

find() does not only take single value as parameter like belowPost::find(1); find() method also can accept array like belowPost::find([1,2]) //...


Using findorFail() and firstOrFail() method

Without findorFail() method we need to implement like following snippet$post = Post::find(12333); if(!$post) { abort(404); } We can implement the...


Using whereDate, whereYear, whereMonth, whereDay and whereCreatedAt for dates

Following snippets would be able to fetch records based on given magic where() methods.01. whereCreatedAt - using exact value of...


Using brackets to specify two or more where clauses into its own where function in eloquent

Suppose we need to fetch records from post table where user id of post needs to be 1 and they...


Using local scope in eloquent

If there are repeating eloquent conditions in different methods and we can group and reuse those in different methods of...


Using global scope in eloquent

Like local scopes for repeating eloquent conditions we can use global scope also to all queries of a given model....