Last active
January 6, 2023 09:21
-
-
Save pmkay/39dfb9855b3cff82fc12770f2aea4e73 to your computer and use it in GitHub Desktop.
Using multiple keys for route model binding in Laravel
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
public function boot() | |
{ | |
Route::bind('course', function ($value) { | |
return Course::where('slug', $value)->orWhere(function ($query) use ($value) { | |
if (is_numeric($value)) { | |
$query->where('id', $value); | |
} | |
})->firstOrFail(); | |
}); | |
parent::boot(); | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment