Skip to content

Commit afa615b

Browse files
committed
Added getRouteBindingQuery method
1 parent 55be6b3 commit afa615b

File tree

4 files changed

+64
-2
lines changed

4 files changed

+64
-2
lines changed

src/HasSlug.php

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,11 @@ public function resolveRouteBinding($value, $field = null)
166166
$locale = app()->getLocale();
167167

168168
// Return exact match if we find it
169-
if($result = $this->where($key . '->' . $locale, $value)->first()) {
169+
$result = $this->getRouteBindingQueryBuilder()
170+
->where($key . '->' . $locale, $value)
171+
->first();
172+
173+
if($result) {
170174
return $result;
171175
}
172176

@@ -176,7 +180,9 @@ public function resolveRouteBinding($value, $field = null)
176180
}
177181

178182
// Get the models where this slug exists in other langs as well
179-
$results = $this->whereRaw('JSON_SEARCH(`'.$key.'`, "one", "'.$value.'")')->get();
183+
$results = $this->getRouteBindingQueryBuilder()
184+
->whereRaw('JSON_SEARCH(`'.$key.'`, "one", "'.$value.'")')
185+
->get();
180186

181187
// If we have zero or multiple results, don't guess
182188
if($results->count() !== 1) {
@@ -188,4 +194,21 @@ public function resolveRouteBinding($value, $field = null)
188194
Router::current(), $locale, false
189195
)]);
190196
}
197+
198+
/**
199+
* Get a new query builder for the Route Binding
200+
*
201+
* @param $value
202+
* @return mixed
203+
*/
204+
protected function getRouteBindingQueryBuilder()
205+
{
206+
$query = $this->newQuery();
207+
208+
if(method_exists($this, 'getRouteBindingQuery')) {
209+
return $this->getRouteBindingQuery($query);
210+
}
211+
212+
return $query;
213+
}
191214
}

tests/SluggableTest.php

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,4 +77,40 @@ public function test_can_generate_translated_url_for_route()
7777

7878
$this->assertSame('/foo/french-title/some-value', $model->getSluggedUrlForRoute($route, 'fr', false));
7979
}
80+
81+
public function test_can_resolve_model()
82+
{
83+
TestModelTranslated::create([
84+
'title' => [
85+
'en' => 'English title',
86+
'fr' => 'French title'
87+
]
88+
]);
89+
90+
$result = (new TestModelTranslated())
91+
->resolveRouteBinding('english-title');
92+
93+
$this->assertSame('English title', $result ? $result->title : null);
94+
}
95+
96+
public function test_can_resolve_model_with_custom_query()
97+
{
98+
TestModelTranslated::create([
99+
'title' => [
100+
'en' => 'English custom query',
101+
'fr' => 'French custom query'
102+
],
103+
'deleted_at' => now()
104+
]);
105+
106+
$instance = new class() extends TestModelTranslated {
107+
protected function getRouteBindingQuery($query) {
108+
return $query->withTrashed();
109+
}
110+
};
111+
112+
$result = $instance->resolveRouteBinding('english-custom-query');
113+
114+
$this->assertSame('English custom query', $result ? $result->title : null);
115+
}
80116
}

tests/TestCase.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ protected function setUpDatabase()
4747
$table->json('title')->nullable();
4848
$table->json('name')->nullable();
4949
$table->json('slug')->nullable();
50+
$table->softDeletes();
5051
});
5152

5253
$this->app['db']->connection()->getSchemaBuilder()->create('test_model_custom_attributes', function (Blueprint $table) {

tests/TestModelTranslated.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,13 @@
33
namespace Whitecube\Sluggable\Tests;
44

55
use Illuminate\Database\Eloquent\Model;
6+
use Illuminate\Database\Eloquent\SoftDeletes;
67
use Spatie\Translatable\HasTranslations;
78
use Whitecube\Sluggable\HasSlug;
89

910
class TestModelTranslated extends Model
1011
{
12+
use SoftDeletes;
1113
use HasSlug;
1214
use HasTranslations;
1315

0 commit comments

Comments
 (0)