Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/Illuminate/Database/Query/Builder.php
Original file line number Diff line number Diff line change
Expand Up @@ -903,6 +903,8 @@ public function orWhereColumn($first, $operator = null, $second = null)
*/
public function whereRaw($sql, $bindings = [], $boolean = 'and')
{
$sql = '('.$sql.')';

$this->wheres[] = ['type' => 'raw', 'sql' => $sql, 'boolean' => $boolean];

$this->addBinding((array) $bindings, 'where');
Expand Down
13 changes: 13 additions & 0 deletions tests/Database/DatabaseEloquentSoftDeletesIntegrationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -714,6 +714,19 @@ public function testMorphToNonSoftDeletingModel()
$this->assertNull($comment->owner);
}

public function testSoftDeletesCanUseWhereRaw()
{
$this->createUsers();

$builder = SoftDeletesTestUser::whereRaw('id = 1 OR email = "abigailotwell@gmail.com"');
$users = $builder->get();

$this->assertSame('select * from "users" where (id = 1 OR email = "abigailotwell@gmail.com") and "users"."deleted_at" is null', $builder->toSql());
$this->assertCount(1, $users);
$this->assertSame(2, $users->first()->id);
$this->assertNull(SoftDeletesTestUser::find(1));
}

/**
* Helpers...
*/
Expand Down
12 changes: 6 additions & 6 deletions tests/Database/DatabaseQueryBuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -690,15 +690,15 @@ public function testRawWheres()
{
$builder = $this->getBuilder();
$builder->select('*')->from('users')->whereRaw('id = ? or email = ?', [1, 'foo']);
$this->assertSame('select * from "users" where id = ? or email = ?', $builder->toSql());
$this->assertSame('select * from "users" where (id = ? or email = ?)', $builder->toSql());
$this->assertEquals([0 => 1, 1 => 'foo'], $builder->getBindings());
}

public function testRawOrWheres()
{
$builder = $this->getBuilder();
$builder->select('*')->from('users')->where('id', '=', 1)->orWhereRaw('email = ?', ['foo']);
$this->assertSame('select * from "users" where "id" = ? or email = ?', $builder->toSql());
$this->assertSame('select * from "users" where "id" = ? or (email = ?)', $builder->toSql());
$this->assertEquals([0 => 1, 1 => 'foo'], $builder->getBindings());
}

Expand Down Expand Up @@ -1649,7 +1649,7 @@ public function testJoinsWithAdvancedConditions()
->orWhereRaw('year(contacts.created_at) = 2016');
});
});
$this->assertSame('select * from "users" left join "contacts" on "users"."id" = "contacts"."id" and ("role" = ? or "contacts"."disabled" is null or year(contacts.created_at) = 2016)', $builder->toSql());
$this->assertSame('select * from "users" left join "contacts" on "users"."id" = "contacts"."id" and ("role" = ? or "contacts"."disabled" is null or (year(contacts.created_at) = 2016))', $builder->toSql());
$this->assertEquals(['admin'], $builder->getBindings());
}

Expand All @@ -1675,7 +1675,7 @@ public function testJoinsWithSubqueryCondition()
->whereNull('deleted_at');
});
});
$this->assertSame('select * from "users" left join "contacts" on "users"."id" = "contacts"."id" and exists (select 1 from "contact_types" where contact_types.id = contacts.contact_type_id and "category_id" = ? and "deleted_at" is null)', $builder->toSql());
$this->assertSame('select * from "users" left join "contacts" on "users"."id" = "contacts"."id" and exists (select 1 from "contact_types" where (contact_types.id = contacts.contact_type_id) and "category_id" = ? and "deleted_at" is null)', $builder->toSql());
$this->assertEquals(['1'], $builder->getBindings());
}

Expand All @@ -1694,7 +1694,7 @@ public function testJoinsWithAdvancedSubqueryCondition()
});
});
});
$this->assertSame('select * from "users" left join "contacts" on "users"."id" = "contacts"."id" and exists (select 1 from "contact_types" where contact_types.id = contacts.contact_type_id and "category_id" = ? and "deleted_at" is null and "level_id" in (select "id" from "levels" where "is_active" = ?))', $builder->toSql());
$this->assertSame('select * from "users" left join "contacts" on "users"."id" = "contacts"."id" and exists (select 1 from "contact_types" where (contact_types.id = contacts.contact_type_id) and "category_id" = ? and "deleted_at" is null and "level_id" in (select "id" from "levels" where "is_active" = ?))', $builder->toSql());
$this->assertEquals(['1', true], $builder->getBindings());
}

Expand Down Expand Up @@ -2782,7 +2782,7 @@ public function testMySqlWrappingJson()
{
$builder = $this->getMySqlBuilder();
$builder->select('*')->from('users')->whereRaw('items->\'$."price"\' = 1');
$this->assertSame('select * from `users` where items->\'$."price"\' = 1', $builder->toSql());
$this->assertSame('select * from `users` where (items->\'$."price"\' = 1)', $builder->toSql());

$builder = $this->getMySqlBuilder();
$builder->select('items->price')->from('users')->where('users.items->price', '=', 1)->orderBy('items->price');
Expand Down