Skip to content
Merged
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: 1 addition & 1 deletion src/Concerns/HasSqids.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ public function resolveRouteBindingQuery($query, $value, $field = null): Builder
return parent::resolveRouteBindingQuery(query: $query, value: $value, field: $field);
}

return $this->whereSqid(sqid: $value);
return $query->whereSqid(sqid: $value);
}

public static function keyFromSqid(string $sqid): ?int
Expand Down
19 changes: 19 additions & 0 deletions tests/RouteModelBindingTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

declare(strict_types=1);

use Workbench\Database\Factories\ChargeFactory;
use Workbench\Database\Factories\CustomerFactory;
use Workbench\Database\Factories\PostFactory;

Expand Down Expand Up @@ -44,3 +45,21 @@
->get(uri: "/posts/{$post->slug}")
->assertContent(value: $post->title);
});

it('can scope route model bindings', function (): void {
$customer = CustomerFactory::new()->create();
$charge = ChargeFactory::new()->for($customer)->create();

$this
->get(uri: "/customers/{$customer->sqid}/{$charge->sqid}")
->assertContent(value: $charge->sqid);
});

it('returns a 404 if the child isn’t scoped to the parent', function (): void {
$customer = CustomerFactory::new()->create();
$charge = ChargeFactory::new()->create();

$this
->get(uri: "/customers/{$customer->sqid}/{$charge->sqid}")
->assertNotFound();
});
9 changes: 9 additions & 0 deletions workbench/app/Models/Charge.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,20 @@
namespace Workbench\App\Models;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use RedExplosion\Sqids\Concerns\HasSqids;

class Charge extends Model
{
use HasSqids;

protected string $sqidPrefix = 'ch';

/**
* @return BelongsTo<Customer,$this>
*/
public function customer(): BelongsTo
{
return $this->belongsTo(Customer::class);
}
}
9 changes: 9 additions & 0 deletions workbench/app/Models/Customer.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,18 @@
namespace Workbench\App\Models;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\HasMany;
use RedExplosion\Sqids\Concerns\HasSqids;

class Customer extends Model
{
use HasSqids;

/**
* @return HasMany<Charge,$this>
*/
public function charges(): HasMany
{
return $this->hasMany(Charge::class);
}
}
2 changes: 2 additions & 0 deletions workbench/routes/web.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,12 @@
declare(strict_types=1);

use Illuminate\Support\Facades\Route;
use Workbench\App\Models\Charge;
use Workbench\App\Models\Customer;
use Workbench\App\Models\Post;

Route::get(uri: 'customers/username/{customer:username}', action: fn (Customer $customer) => $customer->username);
Route::get(uri: 'customers/{customer}', action: fn (Customer $customer) => $customer->name);
Route::get(uri: 'customers/{customer}/{charge}', action: fn (Customer $customer, Charge $charge) => $charge->sqid)->scopeBindings();

Route::get(uri: 'posts/{post}', action: fn (Post $post) => $post->title);