Home » Query Scope using Laravel 6 Eloquent Example

Query Scope using Laravel 6 Eloquent Example

Today, We want to share with you Query Scope using Laravel 6 Eloquent Example.In this post we will show you How to Create and Use Query Scope in Laravel Eloquent, hear for laravel add scope to query builder we will give you demo and example for implement.In this post, we will learn about laravel custom query builder method with an example.

Query Scope using Laravel 6 Eloquent Example

There are the Following The simple About Re-using query constraints in Laravel 6 Eloquent Full Information With Example and source code.

Read Also:  Vue.js onclick Event Handling Example

As I will cover this Post with live Working example to develop Advanced Laravel Eloquent usage, so the Make custom query builder method (query scope) for all models in Laravel 6.2 is used for this example is following below.

Step 1: Create Laravel Scope in Model

app/Movie.php

<?php
  
namespace App;
  
use Illuminate\Database\Eloquent\Model;
  
class Movie extends Model
{
    public $table = "movies";
      
    /**
     * 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 scopeToday($query)
    {
        return $query->whereDate('created_at', \Carbon\Carbon::today());
    }
}

Step 2: Laravel 6 Use Scope Query

Laravel 6 controller file

Movie::select("*")->today()->get();

Step 3 : Laravel 6 Dynamic Scope in Model

app/Movie.php

<?php
  
namespace App;
  
use Illuminate\Database\Eloquent\Model;
  
class Movie extends Model
{
    public $table = "movies";
      
    /**
     * 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);
    }
}

Step 4: Laravel Use Scope Query

use Laravel controller file

Movie::select("*")->status(1)->get();

Web Programming Tutorials Example with Demo

Read :

Read Also:  laravel 6 Update Data in AJAX CRUD Operations

Summary

You can also read about AngularJS, ASP.NET, VueJs, PHP.

I hope you get an idea about laravel update or create.
I would like to have feedback on my infinityknow.com blog.
Your valuable feedback, question, or comments about this article are always welcome.
If you enjoyed and liked this post, don’t forget to share.


Related FAQ

Here are some more FAQ related to this Article:

  1. Read Also:  Angular Multiple Carousel Image Slider
  2. Read Also:  PHP Laravel Soft Delete Migration cascade
  3. Read Also:  Laravel 6 Deletes method using Eloquent ORM
  4. Read Also:  How to create model object in controller in laravel?
  5. Read Also:  Remove Last Comma string in javascript
  6. Read Also:  Laravel Complex queries using Eloquent Query
  7. Read Also:  Laravel Collection Contains with function
  8. Read Also:  Jquery check if value exists in Array Example
  9. Read Also:  How to Create Model In Laravel 6 Example?
  10. Read Also:  Laravel 6 Eloquent Global Scope Tutorial Example

Leave a Comment

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