Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fractal presenters: Manually include relationship presenter #101

Closed
jonagoldman opened this issue Nov 1, 2015 · 3 comments
Closed

Fractal presenters: Manually include relationship presenter #101

jonagoldman opened this issue Nov 1, 2015 · 3 comments

Comments

@jonagoldman
Copy link

I want to include relationships in my presenter.
I know I can _automatically_ include default relationships by adding this to my Transformer class:

class BookTransformer extends TransformerAbstract
{
    protected $defaultIncludes = [
        'author'
    ];

    // ...
}

Now, I want to choose _manually_ when to add a relationship.
Reading the Fractal docs I see that I need to add this to my Transformer class

class BookTransformer extends TransformerAbstract
{

    protected $availableIncludes = [
        'author'
    ];

    public function transform(Book $book)
    {
        return [
           //  ...
        ];
    }

    public function includeAuthor(Book $book)
    {
        $author = $book->author;
        return $this->item($author, new AuthorTransformer);
    }
}

But now I need a way to tell the presenter to include my relationship ('author' in this case)

How can I do that?

@jonagoldman
Copy link
Author

My current solution:

_Step 1:_ Create new class that extends FractalPresenter and override the parseIncludes() method to allow $includes parameter:

use Prettus\Repository\Presenter\FractalPresenter;

abstract class FractalRelationshipsPresenter extends FractalPresenter
{

    public function parseIncludes($includes = array())
    {

        $request        = app('Illuminate\Http\Request');
        $paramIncludes  = config('repository.fractal.params.include','include');

        if ( $request->has( $paramIncludes ) ) {
            $includes = array_merge($includes, explode(',', $paramIncludes));
        }

        $this->fractal->parseIncludes($includes);

        return $this;
    }

}

_Step 2:_ Create new class that extends BaseRepository and create present() method to allow to add relationships to the presenter:

use Prettus\Repository\Eloquent\BaseRepository;

abstract class PresentableRepository extends BaseRepository
{
    public function present($relations)
    {
        $this->presenter->parseIncludes($relations);
        return $this;
    }

}

_Step 3:_ Update your Presenters and Repositories to extend the new classes in previous steps (I'm assuming a Transformer was already created with all desired relationships):

class MyPresenter extends FractalRelationshipsPresenter
{
    public function getTransformer()
    {
        return new MyTransformer();
    }
}
class MyRepositoryEloquent extends PresentableRepository implements MyRepository
{
    public function model()
    {
        return MyModel::class;
    }

    public function presenter()
    {
        return MyPresenter::class;
    }
}

_Usage:_ Call the present() method to include presenter relationships:

class MyController extends Controller
{

    protected $repository;

    public function __construct(MyRepository $repository){
        $this->repository = $repository;
    }

    public function index()
    {
        $collection = $this->repository->present(['myrelationship'])->all();
    }
}

@andersao
Copy link
Owner

I've been very busy in recent months with my work projects, which eventually pulling away of my personal projects, so it could not provide the necessary support to you, I am more relaxed now and I'm checking all the issues in order to work on fixes and new implementations, apologize to everyone.

@ghost ghost closed this as completed Apr 12, 2016
@pat-och
Copy link

pat-och commented Sep 4, 2018

first thanks @jonagoldman for this nice tip !

Second, is possible to implement this in the with() repository function instead making a new present() function ?

attempt is to have more standart behavior who manage the needed relations in background

This issue was closed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants