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
5 changes: 4 additions & 1 deletion src/Illuminate/Database/Query/Builder.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)
)
);
}

Expand Down
15 changes: 15 additions & 0 deletions src/Illuminate/Database/Query/Grammars/Grammar.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down
10 changes: 10 additions & 0 deletions tests/Database/DatabaseEloquentPolymorphicIntegrationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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...
*/
Expand Down Expand Up @@ -172,6 +181,7 @@ public function posts()
class TestPost extends Eloquent
{
protected $table = 'posts';
protected $withCount = ['comments'];
protected $guarded = [];

public function comments()
Expand Down
17 changes: 17 additions & 0 deletions tests/Integration/Database/EloquentWithCountTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down