Last active
November 15, 2021 18:52
-
-
Save marksparrish/e35e9686bf95f608f8ef2e8c8eeba857 to your computer and use it in GitHub Desktop.
Laravel ElasticSearch Findable Trait
This file contains 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 | |
namespace App\Elastic; | |
use Illuminate\Support\Str; | |
/** @package App\Elastic */ | |
trait Findable | |
{ | |
public static function finder() | |
{ | |
// get the current model | |
$model = (new static); | |
// make the builder string | |
$builder = __NAMESPACE__ . '\\Builders\\' . Str::studly(Str::singular($model->getTable())) . 'Builder' ; | |
if (class_exists($builder)) { | |
return app($builder,['model' => $model]); | |
} else { | |
// generic builder if there is no associated model builder class | |
$builder = __NAMESPACE__ . '\\Builders\\Builder' ; | |
return app($builder,['model' => $model]); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment