phplaravel-4modelsextendsextending-classes

Extending a package model


I have installed Cmgmyr\Messenger, however, I need to extend the Thread model and potentially a couple of the others as my users table does not contain a name field.

The method I need to extend is:

/**
     * Generates a string of participant information
     *
     * @param null $userId
     * @param array $columns
     * @return string
     */
    public function participantsString($userId=null, $columns=['name'])
    {
        $selectString = $this->createSelectString($columns);

        $participantNames = $this->getConnection()->table('users')
            ->join('participants', 'users.id', '=', 'participants.user_id')
            ->where('participants.thread_id', $this->id)
            ->select($this->getConnection()->raw($selectString));

        if ($userId !== null) {
            $participantNames->where('users.id', '!=', $userId);
        }

        $userNames = $participantNames->lists('users.name');

        return implode(', ', $userNames);
    }

Notice the users.name filed being called? This is what needs to be changed to username or even better users.firstname and users.lastname together.

I need to extend it to the following structure:

Modules/
 - Email/
   - Models/
    - Thread.php

How can I go about this?


Solution

  • The easiest way is to fork the package on github, make the changes to the package yourself, then pull in your custom package on composer (instead of the original package).

    This way you maintain control over your changes on the other package.

    The specific method for pulling in your fork is covered here: How to require a fork with composer