Skip to content

[9.x] Different hasMany/SoftDelete behaviour between Laravel 8.x and 9.x #40400

@nickurt

Description

@nickurt
  • Laravel Version: 8.79.0 vs 9.x (beta-1)
  • PHP Version: 8.0.13
  • Database Driver & Version: MySql 5.7

Description:

The MySql (5.7) Query is different between Laravel 8.79.0 and 9.x (beta-1), by 8.x it will include 'and posts.deleted_at is null' at the end of the query, while it's gone by 9.x.

class User extends Authenticatable
{
    //

    public function posts()
    {
        return $this->hasMany(Post::class);
    }
}

class Post extends Model
{
    use \Illuminate\Database\Eloquent\SoftDeletes;

    //
}


class ExampleTest extends TestCase
{
    public function test()
    {
        $user = new \App\Models\User;

        // Expected (Laravel 8) : 'select * from `posts` where `posts`.`user_id` is null and `posts`.`user_id` is not null and `posts`.`deleted_at` is null'
        // Actual (Laravel 9)   : 'select * from `posts` where `posts`.`user_id` is null and `posts`.`user_id` is not null'

        $this->assertSame('select * from `posts` where `posts`.`user_id` is null and `posts`.`user_id` is not null and `posts`.`deleted_at` is null', $user->posts()->toSql());
    }
}

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