Skip to content

Instantly share code, notes, and snippets.

@rentalhost
Created May 13, 2019 07:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rentalhost/a4b2446e3c772a3c14ababd990e5c7a9 to your computer and use it in GitHub Desktop.
Save rentalhost/a4b2446e3c772a3c14ababd990e5c7a9 to your computer and use it in GitHub Desktop.
Laravel #28506
<?php
declare(strict_types = 1);
namespace App;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
class Example
extends Model
{
use SoftDeletes;
}
<?php
declare(strict_types = 1);
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
class ExamplesTable
extends Migration
{
public function down(): void
{
Schema::dropIfExists('examples');
}
public function up(): void
{
Schema::create('examples', static function (Blueprint $table) {
$table->bigIncrements('id');
$table->timestamps();
$table->softDeletes();
});
}
}
<?php
declare(strict_types = 1);
namespace Tests\Unit;
use App\Example;
use Illuminate\Support\Facades\DB;
use Tests\TestCase;
class ExampleTest
extends TestCase
{
public function testQuery(): void
{
DB::table('examples')->truncate();
// Example case.
$example = new Example;
$example->deleted_at = now();
$example->save();
// OK:
$this->assertSame(/** @lang text */ 'select * from `examples` where `examples`.`deleted_at` is null', Example::query()->toSql());
// Fail: it returns only "select * from `examples`", ignoring the SoftDeletes -- and all others -- scopes.
$this->assertSame(/** @lang text */ 'select * from `examples` where `examples`.`deleted_at` is null', Example::query()->getQuery()->toSql());
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment