Laravel – Get instance of repository interface in migration

  laravel, laravel-8, php

I’m using Laravel (Vers. 8.x) for a project. I’m struggeling to get an instance of my repository interface. I assume it is possible to get an instance of a repository interface, but actually I did not get it to work. Maybe someone has a hint for me.

Here is my code:
I have a standard migration created with

$ php artisan make:migration copy_guests_landing_page_db_entries_from_landing_page1

So this is the migration file:

<?php

use IlluminateDatabaseMigrationsMigration;
use IlluminateDatabaseSchemaBlueprint;
use IlluminateSupportFacadesSchema;

class CopyGuestsLandingPageDbEntriesFromLandingPage1 extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        //
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        //
    }
}

Nothing special yet.
Now I have a repository and an implementation configured. Here are the classes.

Repository interface:

<?php

namespace AppRepositoriesGuest;

interface GuestLandingPageRepository
{
    /**
     * @param mixed $id
     * 
     * @return AppModelsGuestGuestLandingPage
     */
    public function findOrFail($id);

    /**
     * @param array $data
     * 
     * @return AppModelsGuestGuestLandingPage
     */
    public function create($data = []);

    /**
     * @return AppModelsGuestGuestLandingPage[]
     */
    public function all();

    /**
     * @return int
     */
    public function count();

    /**
     * Get guest by identifier token
     * @param string $token 
     * @return AppModelsGuestGuestLandingPage
     */
    public function getByIdentifierToken($token);

    /**
     * Get guest by identifier token
     * @param int $guestId 
     * @param int $landingPageId 
     * @return AppModelsGuestGuestLandingPage
     */
    public function getByGuestIdAndLandingPageId($guestId,$landingPageId);
}

Implementation

<?php

namespace AppRepositoriesGuest;

class GuestLandingPageRepositoryImpl extends AppRepositoriesBaseRepository implements AppRepositoriesGuestGuestLandingPageRepository {
    public function __construct(AppModelsGuestGuestLandingPage $guestLandingPage) {
        parent::__construct($guestLandingPage);
    }

    /**
     * Get guest by identifier token
     * @param string $token 
     * @return AppModelsGuestGuestLandingPage
     */
    public function getByIdentifierToken($token) {
        return $this->model->where('identifier_token', $token)->get()->first();
    }

    /**
     * Get guest by identifier token
     * @param int $guestId 
     * @param int $landingPageId 
     * @return AppModelsGuestGuestLandingPage
     */
    public function getByGuestIdAndLandingPageId($guestId,$landingPageId) {
        return $this->model->where('guest_id', $guestId)->where('landing_page_id',$landingPageId)->get()->first();
    }
}

I also registered it in the RepositoriesServiceProviders class (in the register method).

<?php

namespace AppProviders;

class RepositoriesServiceProvider extends IlluminateSupportServiceProvider
{
    public function register() {
        ....
        //GuestLandingPage
        $this->app->bind(AppRepositoriesGuestGuestLandingPageRepository::class, AppRepositoriesGuestGuestLandingPageRepositoryImpl::class);
        ...
    }
}

…and added this RepositoriesServiceProviders class in the configapp.php.
So everything fine configured and tested. If I use this interface in an controller class, I get the implementation of the interface. So far, so good.

But how can I manage to use this in a migration. I’m sure this is simple, but I actually did not get it to work. If I try to use…

$this->app->make('AppRepositoriesGuestGuestLandingPage');

…in the up() method of the migration, I get an error, that the class cannot be instiated.
So maybe there is a different way of doing this?
Many thanks for any help in advance!

Source: Laravel

Leave a Reply