Skip to content

Calling toBase() on relationships removes soft-deleted condition in Laravel 9 compared to Laravel 8 #41885

@tpetry

Description

@tpetry
  • Laravel Version: 9.7.0
  • PHP Version: 8.1.1

Description:

In Laravel 9 the toBase() function behaves differently than in Laravel 9 when used on a relationship: The deleted_at IS NULL condition of the SoftDeletes scope was still applied to relationship queries in Laravel 8 when calling toBase(), but in Laravel 9 the condition is missing. However calling toBase() on a normal Eloquent query still retains the deleted_at IS NULL condition of the SoftDeletes trait in Laravel 9.

Steps To Reproduce:

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Database\Eloquent\SoftDeletes;

class ModelA extends Model
{
    use SoftDeletes;
    
    public function modelsB(): HasMany
    {
        return $this->hasMany(ModelB::class);
    }
}

class ModelB extends Model
{
    use SoftDeletes;
}

Model::unguard();
$modelA = new ModelA(['id' => 1]);

// Laravel 8: [...] where [...] and "model_b_s"."model_a_id" is not null and "model_b_s"."deleted_at" is null
// Laravel 9: [...] where [...] and "model_b_s"."model_a_id" is not null and "model_b_s"."deleted_at" is null
$modelA->modelsB()->dump();

// Laravel 8: [...] where [...] and "model_b_s"."model_a_id" is not null and "model_b_s"."deleted_at" is null
// Laravel 9: [...] where [...] and "model_b_s"."model_a_id" is not null and "model_b_s"."deleted_at" is null
$modelA->modelsB()->getQuery()->dump();

// Laravel 8: [...] where [...] and "model_b_s"."model_a_id" is not null and "model_b_s"."deleted_at" is null
// Laravel 9: [...] where [...] and "model_b_s"."model_a_id" is not null
$modelA->modelsB()->toBase()->dump();

// Laravel 8: [...] where "model_a_s"."deleted_at" is null
// Laravel 9: [...] where "model_a_s"."deleted_at" is null
$modelA->newQuery()->toBase()->dump();

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions