Laravel Query Scope Example Tutorial

Laravel Query Scope Example Tutorial

Laravel model query scope example tutorial. Here you will learn how to create and use laravel eloquent model query scopes. Also learn how to create and use dynamic query scope in laravel web application.

When you work with small and large laravel web application. At that time you need to get/fetch active records from the database tables using the laravel query scopes.

Laravel query scopes to save your time to write code again and again. You can easily reuse laravel query scopes.

Laravel Model Query Scopes

Create Basic Scope in Model

Now, you will learn how to create and use scope in your laravel model. You could do like:

Go to app/Post.php and create a scope here:

<?php
  
namespace App;
  
use Illuminate\Database\Eloquent\Model;
  
class Post extends Model
{
    public $table = "posts";
      
    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = [
        'id', 'title', 'body', 'status'
    ];
    /**
     * Scope a query to only include popular users.
     *
     * @param \Illuminate\Database\Eloquent\Builder $query
     * @return \Illuminate\Database\Eloquent\Builder
     */

    public function scopeStatus($query)
    {
        return $query->whereDate('status', 1);
    }
}

Use Basic Query Scope on Controller:

You could use model query scope in your laravel web application controllers like:

Post::status()->get(['id','title']);

Create Dynamic Scope in Model

Next, You will learn how to create and use dynamic query scope in laravel web application.

You can create dynamic query scopes in laravel model:

<?php
  
namespace App;
  
use Illuminate\Database\Eloquent\Model;
  
class Post extends Model
{
    public $table = "posts";
      
    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = [
        'id', 'title', 'body', 'status'
    ];
    /**
     * Scope a query to only include popular users.
     *
     * @param \Illuminate\Database\Eloquent\Builder $query
     * @return \Illuminate\Database\Eloquent\Builder
     */
    public function scopeStatus($query, $type)
    {
        return $query->where('status', $type);
    }
}

Use Dynamic Scope Query On Controller

You could use dynamic model query scope in your laravel web application controllers like:

Post::status(1)->get(['id',title']);

Apply Scope to Relationship Laravel

Here, you will learn how to use query scopes with laravel relationship.

Go to your App/Category.php and create a relationship between Categories and Posts Tables:

class Category extends \Eloquent
{
public function posts()
{
return $this->HasMany('Post');
}
}

Go to your App/Post.php and create a relationship between Posts and Categories Tables:

class Post extends \Eloquent
 {
    public function category()
    {
        return $this->belongsTo('Category');
    }
 public function scopePublished($query)
    {
        return $query->where('published', 1);
    }
 }

Use query scope with relationship

$categories = Category::with(['posts' => function ($q) {
$q->published();
}])->get();

Recommended Laravel Tutorial

  1. Laravel Tutorial From Scratch | Step By Step
  2. Laravel Ajax CRUD(DataTables Js) Tutorial Example
  3. Laravel – Ajax CRUD (Operation) Application Example
  4. Laravel 6 Angular JS CRUD Example Tutorial
  5. Upload Files/Images to Amazon s3 Cloud Using Laravel 6 Filesystem
  6. Laravel 6 CKEditor with Image Upload
  7. Ajax Image Upload In Laravel Tutorial Example From Scratch
  8. Laravel 6 Intervention Upload Image Using Ajax – Example
  9. Laravel Upload Image to Database with Validation
  10. Send Email Example Laravel
  11. Generate OR Create PDF In Laravel 6 Example
  12. Simple Generator or Create Qr Code Example Laravel
  13. Laravel Custom Cron Schedule Example Tutorial
  14. Laravel 6 Github Login Example

AuthorTuts Make

My name is Devendra Dode. I am a full-stack developer, entrepreneur, and owner of Tutsmake.com. I like writing tutorials and tips that can help other developers. I share tutorials of PHP, Javascript, JQuery, Laravel, Livewire, Codeigniter, Vue JS, Angular JS, React Js, WordPress, and Bootstrap from a starting stage. As well as demo example.

Leave a Reply

Your email address will not be published. Required fields are marked *