Skip to content

Instantly share code, notes, and snippets.

@marksparrish
Last active November 15, 2021 18:52
Show Gist options
  • Save marksparrish/e35e9686bf95f608f8ef2e8c8eeba857 to your computer and use it in GitHub Desktop.
Save marksparrish/e35e9686bf95f608f8ef2e8c8eeba857 to your computer and use it in GitHub Desktop.
Laravel ElasticSearch Findable Trait
<?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