diff --git a/src/Illuminate/Database/Query/Builder.php b/src/Illuminate/Database/Query/Builder.php index 4feb5862c095..d6f7e5538708 100755 --- a/src/Illuminate/Database/Query/Builder.php +++ b/src/Illuminate/Database/Query/Builder.php @@ -2224,7 +2224,10 @@ public function delete($id = null) } return $this->connection->delete( - $this->grammar->compileDelete($this), $this->getBindings() + $this->grammar->compileDelete($this), + $this->cleanBindings( + $this->grammar->prepareBindingsForDelete($this->bindings) + ) ); } diff --git a/src/Illuminate/Database/Query/Grammars/Grammar.php b/src/Illuminate/Database/Query/Grammars/Grammar.php index 61b6193d947f..21fe1e2c6730 100755 --- a/src/Illuminate/Database/Query/Grammars/Grammar.php +++ b/src/Illuminate/Database/Query/Grammars/Grammar.php @@ -761,6 +761,21 @@ public function compileDelete(Builder $query) return trim("delete from {$this->wrapTable($query->from)} $wheres"); } + /** + * Prepare the bindings for a delete statement. + * + * @param array $bindings + * @return array + */ + public function prepareBindingsForDelete(array $bindings) + { + $cleanBindings = Arr::except($bindings, ['join', 'select']); + + return array_values( + array_merge($bindings['join'], Arr::flatten($cleanBindings)) + ); + } + /** * Compile a truncate table statement into SQL. * diff --git a/tests/Database/DatabaseEloquentPolymorphicIntegrationTest.php b/tests/Database/DatabaseEloquentPolymorphicIntegrationTest.php index cfc01ef384b2..28821354b47c 100644 --- a/tests/Database/DatabaseEloquentPolymorphicIntegrationTest.php +++ b/tests/Database/DatabaseEloquentPolymorphicIntegrationTest.php @@ -119,6 +119,15 @@ public function testItLoadsNestedRelationshipsOnDemand() $this->assertEquals(TestUser::first(), $like->likeable->owner); } + public function testItDeleteRecordIfWithCountNotEmpty() + { + $this->seedData(); + + $like = TestPost::first()->delete(); + + $this->assertNull(TestPost::first()); + } + /** * Helpers... */ @@ -172,6 +181,7 @@ public function posts() class TestPost extends Eloquent { protected $table = 'posts'; + protected $withCount = ['comments']; protected $guarded = []; public function comments() diff --git a/tests/Integration/Database/EloquentWithCountTest.php b/tests/Integration/Database/EloquentWithCountTest.php index dfb9c1b97fd3..71cce9ce59a4 100644 --- a/tests/Integration/Database/EloquentWithCountTest.php +++ b/tests/Integration/Database/EloquentWithCountTest.php @@ -62,6 +62,23 @@ public function it_basic() ['id' => 1, 'twos_count' => 1], ], $results->get()->toArray()); } + + /** + * @test + */ + public function testDeleteWithJoins() + { + Model1::create() + ->twos()->create() + ->threes()->create(); + + Model2::select('two.id') + ->join('one', 'one.id', 1) + ->join('three', 'two.id', 'three.two_id') + ->where('two.id', 1)->delete(); + + $this->assertNull(Model2::find(1)); + } } class Model1 extends Model