Skip to content

withoutGlobalScope and find not working #21953

@Inkognitoo

Description

@Inkognitoo
  • Laravel Version: 5.5.19
  • PHP Version: 7.1.1
  • Database Driver & Version: psql

Description:

I have GlobalScope

<?php

namespace App\Scopes;

use App\Book;
use Illuminate\Database\Eloquent\Scope;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Builder;

class StatusScope implements Scope
{
    /**
     * Apply the scope to a given Eloquent query builder.
     *
     * @param  \Illuminate\Database\Eloquent\Builder  $builder
     * @param  \Illuminate\Database\Eloquent\Model  $model
     * @return void
     */
    public function apply(Builder $builder, Model $model)
    {
        $builder->where('status', Book::OPEN_STATUS);
    }
}

and model

<?php

namespace App;

use App\Scopes\StatusScope;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;

class Book extends Model
{
    use SoftDeletes;

    const OPEN_STATUS = 'open_by_author';

    const CLOSE_STATUS = 'close_by_author';

    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = [
        'title', 'description',
    ];

    /**
     * The attributes that should be mutated to dates.
     *
     * @var array
     */
    protected $dates = ['deleted_at'];

    /**
     * The "booting" method of the model.
     *
     * @return void
     */
    protected static function boot()
    {
        parent::boot();

        static::addGlobalScope(new StatusScope);
    }
}

and when I make
Book::withoutGlobalScope(StatusScope::class)->find($id)
it return sql code like:
select * from "books" where "books"."deleted_at" is null and "status" = ?
and of course it does not working. But when I use where('id, $id) instead find, all okay.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions