Answers for "laravel scope relationship"

PHP
1

laravel scope relationship

class User extends Model {

    public function scopePopular($query)
    {
        return $query->where('votes', '>', 100);
    }

    public function scopeWomen($query)
    {
        return $query->whereGender('W');
    }

}
Posted by: Guest on February-20-2021
0

Eloquent Query Scope on Relationships

Problem :
$activePosts = Post::where('active', true)->get();

Solution: 

class Post extends Model
{
    public function scopeActive($query)
    {
        return $query->where('active', 1);
    }
}

$activePosts = Post::active()->get();
=======================================================

Create Dynamic Scope:

class Post extends Model 
{
    public function scopeActive($query, $value)
    {
        return $query->where('active', $value);
    }
}

// Get active posts
$activePosts = Post::active(true)->get();

// Get not active posts
$notActivePosts = Post::active(false)->get();

===========================================================

Scope with Relation :

$category = Category::find(1);
$activePost = $category->posts()->active(true)->get();
Posted by: Guest on July-16-2020
0

laravel scope

public function apply(Builder $builder, Model $model)
    {
        $builder->where('age', '>', 200);
    }
Posted by: Guest on December-06-2020
1

laravel scope query

$model = App\Models\Flight::where('legs', '>', 100)
            ->firstOr(['id', 'legs'], function () {
                // ...
            });
Posted by: Guest on September-25-2020
0

laravel scope relationship

$users = User::popular()->women()->orderBy('created_at')->get();
Posted by: Guest on February-20-2021
0

laravel scope relationship

class User extends Model {

    protected function getDateFormat()
    {
        return 'U';
    }

}
Posted by: Guest on February-20-2021

Browse Popular Code Answers by Language