samedi 24 mars 2018

Building Model Using Factory Laravel

I am trying to build/factor model using Service Provider but I seem to be missing a step or two to make it work.

Below is my service provider

class TranslationProvider extends ServiceProvider
{
    /**
     * Bootstrap the application services.
     *
     * @return void
     */
    public function boot()
    {

        $this->app->bind(TranslatableModel::class, function($app){
            $translation =  \App::make(Repository::class);
            $model =  (new TranslatableModel())->setTranslationRepository($translation);
            return $model;
        });

    }

    public function register()
    {
    }
}

and here is my TranslatableModel

class TranslatableModel extends Model
{
    use Translatable, TranslationTrait;

    /** @var \App\Repositories\Translation\Repository $translationRepository */
    public $translationRepository;

    public function __construct(array $attributes = array())
    {
        parent::__construct($attributes);
    }

    public function setTranslationRepository(Repository $repo)
    {
        $this->translationRepository = $repo;

        return $this;
    }
}

I know I am trying to inject a Repository into model, and I know the case should be the opposite, However my logic relies on this kind of scenario. I appreciate your help.

Thanks



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire