Eloquent ORM

It is object relational mapper that provides smooth interaction with database.
 

Using dynamic condition under auth check and request query

$query = Post::query(); if ( Auth::check() ) { $category = Category::pluck('id')->toArray(); //dd($category); $categories = Category::get(); $recentPosts = Post::take(8)->orderBy( 'created_at', 'desc'...


A guide on eloquent many to many relationship using pivot table

Suppose a tag has many posts and a post is also associatd with many tags.Hence we can create tag_post table...


Using "with" in eloquent query

This is generally used for eager loading for associated model in the main model which solves N+1 query problem.So, It...


Getting last inserted id

Following approach can be done to get the last inserted id of a model. Step 01: Using mass assignment in...


Running Artisan command for a specific seeder

Step 01: Specifying migration definition of comments tableSchema::create('comments', function (Blueprint $table) { $table->id(); $table->text('comment_text'); $table->timestamps(); }); Step 02: Specifying data...


Selecting last row of a table

It can be done using latest() method. Here function parameter can be empty or it can have field name upon...


How to disable saving of eloquent timestamps

Basically timestamp saving means storing date data into created_at, modified_at or deleted_at fields in table of laravel database during resource...


How to write multiple AND queries inside OR query using closure

Sometimes it requires to setup eloquent builder to create queries which has multiple ands inside a or query. For example,...


Returning rows where a column is between two values

If we were to get posts between ids of 5 and 8 including both of these values we can use...


Selecting rows if they are NULL or NOT NULL

Following snippet will help you to get rows from a table which are either NULL or NOT NULL // rows...